RegExp replace a specific XML/HTML TAG in PHP -



RegExp replace a specific XML/HTML TAG in PHP -

i have little script replace text, come xml file, example:

<b>hello world,</b> <include file="dynamiccontent.php" /> <img src="world.png" /> lot of <i>stuff</i>

obviosly string much more longer, replace <include file="*" /> content of script in filename, @ time utilize explode find

<include file="

but think there improve method solve it. code:

$arrcontent = explode("<include ", $this->objcontent->content); foreach ($contenuto $piece) { // parsing array find include $startposinclude = stripos($piece, "file=\""); // taking position of string file=" if ($startposinclude !== false) { // there 1 $endposinclude = stripos($piece, "\"", 6); $file = substr($piece, $startposinclude+6, $endposinclude-6); $include_file = $file; require ($include_file); // including file $piece = substr($piece, $endposinclude+6); } echo $piece; }

i'm sure regexp done can replacement.

edited allow multiple includes , file checking.

$content = '<b>hello world,</b> <include file="dynamiccontent.php" /> <img src="world.png" /> lot of <i>stuff</i>'; preg_match_all('!<include file="([^"]+)" />!is', $content, $matches); if(count($matches) > 0) { $replaces = array(); foreach ($matches[1] $file) { $tag = '<include file="'.$file.'" />'; if(is_file($file) === true) { ob_start(); require $file; $replaces[$tag] = ob_get_clean(); } else { $replaces[$tag] = '{include "'.$file.'" not found!}'; } } if(count($replaces) > 0) { $content = str_replace(array_keys($replaces), array_values($replaces), $content); } } echo $content;

php xml regex tags

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -