How to remove php code from a string?

I have a line in which there is php code, I need to remove the PHP code from the line, for example:

<?php $db1 = new ps_DB() ?><p>Dummy</p> 

Must return <p>Dummy</p>

And a line without php, for example <p>Dummy</p> , should return the same line.

I know that this can be done using regular expression, but after 4 hours I did not find a solution.

+6
php preg-replace
source share
4 answers
  <?php function filter_html_tokens($a){ return is_array($a) && $a[0] == T_INLINE_HTML ? $a[1]: ''; } $htmlphpstring = '<a>foo</a> something <?php $db1 = new ps_DB() ?><p>Dummy</p>'; echo implode('',array_map('filter_html_tokens',token_get_all($htmlphpstring))); ?> 

As ircmaxell noted: this will require working PHP!

The regex route will be (unless you specify "php" with short tags, and not end with?> In a line / file (for some reason, Zend recommends this?) And, of course, the UNgreedy and DOTALL pattern:

 preg_replace('/<\\?.*(\\?>|$)/Us', '',$htmlphpstring); 
+8
source share

If you use PHP, you just need to use a regular expression to replace anything that matches the PHP code.

The following statement will remove the PHP tag:

 preg_replace('/^<\?php.*\?\>/', '', '<?php $db1 = new ps_DB() ?><p>Dummy</p>'); 

If he does not find any coincidence, he will not replace anything.

0
source share

Well, you can use DomDocument to do this ...

 function stripPHPFromHTML($html) { $dom = new DomDocument(); $dom->loadHtml($html); removeProcessingInstructions($dom); $simple = simplexml_import_dom($d->getElementsByTagName('body')->item(0)); return $simple->children()->asXml(); } function removeProcessingInstructions(DomNode &$node) { foreach ($node->childNodes as $child) { if ($child instanceof DOMProcessingInstruction) { $node->removeChild($child); } else { removeProcessingInstructions($child); } } } 

These two functions will turn into

 $str = '<?php echo "foo"; ?><b>Bar</b>'; $clean = stripPHPFromHTML($str); $html = '<b>Bar</b>'; 

Edit: Actually, looking at the Wrikken answer, I realized that both methods have a flaw ... Mine requires some correct HTML markup (Dom is decent, but it will not parse <b>foo</b><?php echo $bar ) Wrikken requires valid PHP (any syntax errors and this will fail). So maybe this is a combination of the two (try first if this does not work, try the other. If both fail, there really isn’t much you can do without trying to figure out the exact reason they failed) ...

0
source share

A simple solution is to explode into arrays using php tags to remove any content between them and return back to the string.

 function strip_php($str) { $newstr = ''; //split on opening tag $parts = explode('<?',$str); if(!empty($parts)) { foreach($parts as $part) { //split on closing tag $partlings = explode('?>',$part); if(!empty($partlings)) { //remove content before closing tag $partlings[0] = ''; } //append to string $newstr .= implode('',$partlings); } } return $newstr; } 

This is slower than the regular expression, but does not require valid html or php; it requires all php tags to be closed.

For files that do not always include a closing closing tag and for general error checking, you can count tags and add a closing tag if it is missing or notify if opening and closing tags do not stack as expected, for example. add the code below at the beginning of the function. This will slow it down a bit more if :)

  $tag_diff = (substr_count($str,'<?') - (substr_count($str,'?>'); //Append if there one less closing tag if($tag_diff == 1) $str .= '?>'; //Parse error if the tags don't add up if($tag_diff < 0 || $tag_diff > 1) die('Error: Tag mismatch. (Opening minus closing tags = '.$tag_diff.')<br><br> Dumping content:<br><hr><br>'.htmlentities($str)); 
0
source share

All Articles