regex - PHP preg_replace string with value of variable -
i trying use regex in php find strings starts "$" , replacing appropriate variable. example:
$value = "some value"; <b>$value</b>
where end result should be:
<b>some value</b>
this have tried:
ob_start("tagreplace"); function tagreplace ( $tagreplace ) { $value1 = "some value"; $pattern = '/(?<=\s)(\$[^#\s]+)(?=\s)/'; $tagreplace = preg_replace( $pattern , '<?php echo $0 ?>', $tagreplace); return $tagreplace; } /* html files combining */ include 'main_content.html'; ob_end_flush();
but doesn't seem work... i've tried loops, while loops, , none of them working. trying extremely lazy , use regex make shorthand variables can put everywhere, replacing strings "$value"
whatever corresponding value variable $value
has assigned it.
i understand /e
deprecated can't use treat php tags actual code. have attempted use preg_replace_callback
didn't anything. i'm thinking there isn't way this.
it's not clear you're trying here, although if take different approach regex you'll have more success. it's easier break pattern 3 capture groups (the start tag, value, , end tag)
function tagreplace ( $tagreplace ) { $replace = "some value"; $pattern = '/(.+>)(.+)(<.+)/m'; $tagreplace = preg_replace( $pattern, '$1'.$replace.'$3', $tagreplace); return $tagreplace; } $value = "old value"; echo tagreplace("<b>$value</b>"); echo "\n -- using \$value different-- \n"; $value = "<b>$value</b>"; echo tagreplace($value);
by using more generic pattern should able swap out $value
, regardless if it's entire value, or if it's between < >
tags. when in doubt use regex tester (linked below) , might able eliminate several recursive loops, or going around in circles.
example:
Comments
Post a Comment