用preg_replace替换字符

问题描述 投票:0回答:1

使用PHP,我需要改变它

^0123456789...$

对此

0123456789xxx

  1. 在开始时提取可选的^
  2. 在字符串的末尾提取可选的$
  3. 替换所有.x

只有一个ER可能吗?

php regex preg-replace
1个回答
3
投票

最简单的方法是使用str_replace,不需要正则表达式:

$string = '^0123456789...$';
echo str_replace(array('.', '^', '$'), array('x', '', ''), $string);

输出:

0123456789xxx
© www.soinside.com 2019 - 2024. All rights reserved.