php - Replace Number in RegEx with same Number -
php - Replace Number in RegEx with same Number -
<?php function rg($str) { $p = '/\[pattern[0-9]\:[a-za-z]{3,10}\]/'; $r = '[pattern[0-9]]'; $str = preg_replace($p, $r, $str); homecoming $str; } $s = "[pattern1:reverse] test [pattern1:shift] string [pattern3:nitro] reg ex [pattern2:reverse]"; echo rg($s);
the output of be
[pattern[0-9]] test [pattern[0-9]] string [pattern[0-9]] reg ex [pattern[0-9]]
however trying replace number associated pattern is. output like:
[pattern1] test [pattern1] string [pattern3] reg ex [pattern2]
any hints?
try this:
function rg($str) { $p = '/(\[pattern[0-9])\:[a-za-z]{3,10}\]/'; $r = '$1]'; # $1 contains tou grab within paranthesis $str = preg_replace($p, $r, $str); homecoming $str; }
hope helps :)
php regex
Comments
Post a Comment