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 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?

Thanks,

Daniel
Technical Lead
interactivetools.com

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