php - PHP7 Code now doesn't work anymore -


this code used remove bad words text entered users on forum. somehow code not correctly interpreted , remove string empty. know what's wrong. worked fine until php5.6

$message = str_replace('\"', '"', substr(@preg_replace('#(\>(((?' . '>([^><]+|(?r)))*)\<))#se', "@preg_replace(\$orig_word, \$replacement_word, '\\0')", '>' . $message . '<'), 1, -1)); 

that's not code can't understand procedure behind str_replace substr , 2 preg_replace. if transform code callback function or @ least working 1 make me happy.

you can replace with:

$message = preg_replace_callback('~(?<=>|^)[^<>]*+(?=<|$)~',                                  function ($m) use ($orig_word, $replacement_word) {     return str_replace ($orig_word, $replacement_word, $m[0]); }, $message); 

however, doubt code waterproof since doesn't take account of word boundaries , if searched word or isn't between <..>, , since looks crappy approach edit text parts in html code.

a dom approach better. like:

libxml_use_internal_errors(true)  $dom = new domdocument; $dom->loadhtml('<div>' . $message . '</div>', libxml_html_no_implied, libxml_html_no_defdtd); $xp = new domxpath($dom);  $textnodes = $xp->query('//text()');  foreach($textnodes $textnode) {     $newtext = str_replace($orig_word, $replacement_word, $textnode->nodevalue);     // or better if $orig_word contains word characters:     // $newtext = preg_replace("~\b$orig_word\b~", $replacement_word, $textnode->nodevalue);      $textnode->parentnode->replacechild($dom->createtextnode($newtext), $textnode); }  $message = '';  foreach ($dom->documentelement->childnodes $childnode)     $message .= $childnode->savehtml();  libxml_clear_errors(); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -