Removing Quotation from MaxWords

4 posts by 2 authors in: Forums > CMS Builder
Last Post: June 10, 2022   (RSS)

By KennyH - June 9, 2022

I am using maxWords to strip out html tags and limit the content on some JSON data output. However, quotation marks are gumming up the works.

I've tried adding addslashes and

str_replace('"', "", $string);

with no luck

  function maxWords($textOrHtml, $maxWords) { 
  $text = strip_tags($textOrHtml); 
  $words = preg_split("/\s+/", $text, $maxWords+1); 
  if (count($words) > $maxWords) { unset($words[$maxWords]); } 
  $output = join(' ', $words); 
 
  return $output; 
    
  } 

Help?

KennyH

By KennyH - June 10, 2022

Hi Daniel,

It has to do with validation at https://validator.schema.org/

If any of the content in the schema contains quotation marks, it fails to pass validation. When I remove the quotation marks from the content, it passes without any issues. I also needed all other HTML tags to be stripped out and I already use the maxWords script posted here in the forums to truncate content on all of the websites I build, so I just figured using it here would work just as well. It works great (stripping out all of the html tags) but now I think I need it to remove the quotation marks if I am going to use it for the purpose of building JSON schemas.

I also think the issue is with rounded opening/closing double quotes, but not straight double quotes. I think this happens when the content is cut and pasted from a word processor into the WYSIWYG instead of typing it there.

The validation error is always JSON-LD Missing ',' or '}' in object declaration.

I have two pages that are exactly the same except one doesn't have the quotation marks. It passes validation. The one with quotation marks in the content fails validation.

Thanks,

Kenny

By daniel - June 10, 2022

Hi Kenny,

It can be difficult to diagnose this without seeing the content itself, but here is some info I found on escaping JSON strings - something here may help:

https://stackoverflow.com/questions/7462394/php-json-string-escape-double-quotes-for-js-output

If it is an issue with the "fancy" quotation marks, you may need to explicitly remove or escape the characters that are causing issues, since something like this:

str_replace('"', "", $string);

Will only remove standard double quotes, so you'd need to add an extra str_replace() step for each additional character you want to remove. (It's also possible to use arrays to remove multiple characters with a single str_replace() call, examples in the PHP docs: https://www.php.net/manual/en/function.str-replace.php)

Let me know if you have any other questions!

Daniel
Technical Lead
interactivetools.com