preg_replace_callback

mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )

 

  本函数的行为几乎和 preg_replace() 一样,除了不是提供一个 replacement 参数,而是指定一个 callback 函数。该函数将以目标字符串中的匹配数组作为输入参数,并返回用于替换的字符串。

 

例如问题:

preg_replace($skx,$imsz2,$neirong);

如:$neirong中有多个$skx 我需要每次替换都能得到一个不同的ID

 

答案:

<?php $str='this is a test for this string includes many this'; $replace='/this/x'; $result=preg_replace_callback( $replace, function($ms){ static $i; $i=$i+1; return "that($i)"; }, $str ); echo $result,"/n";

你可能感兴趣的:(function,String,callback)