【发布时间】:2017-10-20 05:54:29
【问题描述】:
我需要在我的 PHP 字符串中替换 <...> 中包含的字符。我用preg_replace() 做了这个,但是我需要修改这个代码来做更多的事情。
这是我的代码:
$templateText = "Hi <John> , This is a test message from <9876543210>";
$repl = "test";
$patt = "/\<([^\]]+)\>/";
echo $template_sample = preg_replace($patt, $repl , $templateText);
上面的代码会将<...> 中第一次出现的值替换为test。
例如上面的代码将显示如下字符串:
Hi test
但是,只有当它不是数字时,我才需要将其替换为 test。如果包含的值为数字,则应将其替换为999999999。
我期待的是:
Hi test , This is a test message from 999999999
【问题讨论】:
标签: php regex string preg-replace