tags:

views:

42

answers:

3

Hello,

I want to replace any string before "/", irrespective of the string length.

Thanks Jean

+3  A: 

one way, assuming you want to change the string before the first "/".

$str = "anystring/the_rest/blah";
$s = explode("/",$str);
$s[0]="new string";
print_r ( implode("/",$s) );
ghostdog74
You, know the funny thing is I tried that stuff out, I got my head scratching, stopped short of my balls, then stacked overflow.Thanks
Jean
+2  A: 
echo preg_replace('/^[^\/]+/', 'baz', 'foo/bar');
Ignacio Vazquez-Abrams
A: 

Something like that would be the most efficient, although i still prefer the preg_replace() technique

$pos = strpos($input, '/');
if ($pos >= 0) {
    $output = $replacement . substr($input, $pos);
} else {
    $output = $input;
}
Gruik