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/
3
  • 1
    What changes within the url or are you expecting global always? Id perhaps do it like str_replace('\\','/',stristr($string,'global\\',true)); Sep 1, 2012 at 14:30
  • yes it is always global or local. thanks.
    – Run
    Sep 1, 2012 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 1, 2012 at 14:48

1 Answer 1

0
$string = 'C:\wamp\www\global_tolerance_2012\global\models';
$new_string = str_replace( '\\', '/', $string );
$new_string = preg_replace('#/(local|global)/.*#si', '/', $new_string);
2
  • thanks but it doesnt work I'm afraid...
    – Run
    Sep 1, 2012 at 14:39
  • Try now - I just saw your mention of global/local in the comments. Sep 1, 2012 at 14:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.