如何只用preg_replace匹配整个单词?

时间:2012-09-01 14:20:44

标签: php regex preg-replace

  

可能重复:
  How exactly do Regular Expression word boundaries work in PHP?

如何才能将整个单词与preg_replace()匹配?

$string = 'C:\wamp\www\global_tolerance_2012\global\models'


$new_string = str_replace('\\', '/',preg_replace('#(.*?)(global).*#', '$1', $string));

结果,

C:/wamp/www/

我想要的结果,

C:/wamp/www/global_tolerance_2012/

1 个答案:

答案 0 :(得分:0)

$string = 'C:\wamp\www\global_tolerance_2012\global\models';
$new_string = str_replace( '\\', '/', $string );
$new_string = preg_replace('#/(local|global)/.*#si', '/', $new_string);