Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace any string before "/", PHP

Tags:

php

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

Thanks Jean

like image 821
X10nD Avatar asked Sep 27 '26 23:09

X10nD


2 Answers

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) );
like image 192
ghostdog74 Avatar answered Sep 30 '26 12:09

ghostdog74


echo preg_replace('/^[^\/]+/', 'baz', 'foo/bar');
like image 22
Ignacio Vazquez-Abrams Avatar answered Sep 30 '26 11:09

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!