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
Post a Comment