2014-11-01 71 views
-1

我试图使用str_replace函数在一个字符串使用str_replace函数找到()

$txt = str_replace('[0-9]+','777',$txt); 

而是试图让为数字的数量[0-9]+不工作:(

请问如何实现这种“任意数量”选择?

谢谢:)


编辑:我已经试过了preg_replace,但我无法适应我的代码:(

preg_replace(
"/".$child."\<\/a\> \([0-9]+\)/", 
"/".$child."\<\/a\> \(".ccount($child)."\)/", 
$txt); 

Child</a> (4567) --->Child</a> (8798)

你有一个想法,请?

谢谢!

回答

1
$txt = preg_replace(
    '/(' . preg_quote($child, '/') . '<\/a>) \([0-9]+\)/', 
    '$1 (' . ccount($child) . ')', 
    $txt); 
+0

谢谢,它的工作原理! 1美元是不是像'重复preg_quote'那样的stg?非常感谢 – 2014-11-01 19:07:43

+0

@ bat.t [在替换字符串中编号为'$ n'的引用告诉它在模式中包含相应的'('...')'捕获组的匹配文本。](http:// www.php.net/manual/en/function.preg-replace.php) – Boann 2014-11-01 19:11:41

+0

这解释了preg_quote&Co周围的():)非常感谢解释 – 2014-11-01 19:57:35

2

你应该使用preg_replace

$txt = preg_replace('/[0-9]+/','777', $txt);