skip to Main Content

I’m using OSCommerce for my online store and I’m currently optimizing my product page for rich snippets.
Some of my Google Indexed pages are being marked as “Failed” by Google due to double quotes in the description field.

I’m using an existing code which strips the html coding and truncates anything after 197 characters.

<?php echo substr(trim(preg_replace('/ss+/', ' ', strip_tags($product_info['products_description']))), 0, 197); ?>

How can I include the removal of quotes in that code so that the following string:

<strong>This product is the perfect "fit"</strong>

becomes:

This product is the perfect fit

2

Answers


  1. Happened with me, try to use:

    tep_output_string($product_info['products_description']))
    

    " becomes &quot;

    Login or Signup to reply.
  2. We can try using preg_replace_callback here:

    $input = "SOME TEXT HERE <strong>This product is the perfect "fit"</strong> SOME MORE TEXT HERE";
    $output = preg_replace_callback(
        "/<([^>]+)>(.*?)</\1>/",
        function($m) {
            return str_replace(""", "", $m[2]);
        },
        $input);
    echo $output;
    

    This prints:

    SOME TEXT HERE This product is the perfect fit SOME MORE TEXT HERE
    

    The regex pattern used does the following:

    <([^>]+)>  match an opening HTML tag, and capture the tag name
    (.*?)      then match and capture the content inside the tag
    </\1>    finally match the same closing tag
    

    Then, we use a callback function which does an additional replacement to strip off all double quotes.

    Note that in general using regex against HTML is bad practice. But, if your text only has single level/occasional HTML tags, then the solution I gave above might be viable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search