0

Possible Duplicate:
How exactly do Regular Expression word boundaries work in PHP?

How can I match the whole word only with preg_replace()?

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


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

result,

C:/wamp/www/

the result I want,

C:/wamp/www/global_tolerance_2012/
Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    What changes within the url or are you expecting `global` always? Id perhaps do it like `str_replace('\\','/',stristr($string,'global\\',true));` – Lawrence Cherone Sep 01 '12 at 14:30
  • yes it is always `global` or `local`. thanks. – Run Sep 01 '12 at 14:33
  • You need to give the regex a further hint on only matching complete words, or other surrounding characters that can be used as markers. Right now it does what you asked it to do. It can't guess intentions. – mario Sep 01 '12 at 14:48

1 Answers1

0
$string = 'C:\wamp\www\global_tolerance_2012\global\models';
$new_string = str_replace( '\\', '/', $string );
$new_string = preg_replace('#/(local|global)/.*#si', '/', $new_string);
HappyTimeGopher
  • 1,377
  • 9
  • 14