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; 



By daniel - June 10, 2022

Hi Kenny,

Could you elaborate on the problem being caused by quotation marks? Could you give me an example input string, and how it fails?


Technical Lead

By KennyH - June 10, 2022

Hi Daniel,

It has to do with validation at

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.