3

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

Thanks Jean

1
  • you should show an example of what you want Commented Feb 17, 2010 at 8:51

3 Answers 3

7

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) );
Sign up to request clarification or add additional context in comments.

1 Comment

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
4
echo preg_replace('/^[^\/]+/', 'baz', 'foo/bar');

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.