I want to replace any line before "/", regardless of the length of the line.
Thanks jean
in one case, if you want to change the line before the first "/".
$str = "anystring/the_rest/blah"; $s = explode("/",$str); $s[0]="new string"; print_r ( implode("/",$s) );
echo preg_replace('/^[^\/]+/', 'baz', 'foo/bar');
Something like this would be most efficient, although I still prefer the preg_replace () method
$pos = strpos($input, '/'); if ($pos >= 0) { $output = $replacement . substr($input, $pos); } else { $output = $input; }
Source: https://habr.com/ru/post/1733149/More articles:Where to put certain logic in CakePHP - phpCalculate tree height - c ++ServletRequestWrapper or ServletResponseWrapper in production? - javaIs the single wxWidgets MSW application binary compatible with Win2K, XP, Vista and 7? - windowsHow to pass hidden parameters to "link_to" - ruby-on-railsCufon.refresh () not working in IE8 - javascriptSign a PDF with one signature but with several signatures - c #How to add persistent space to existing HTML using Python? - pythonWhy does my Perl program complain "Cannot find URI.pm in @INC"? - moduleMeasure page load time - performanceAll Articles