Reputation: 3793
I know this has been asked before as ive just been reading those answers but still cant get this to work (properly).
Im very new to regex and am trying to do something that sounds pretty simple:
The string would be: http://www.something.com/section/filter/colour/red-#998682/size/small/
What i would like to do is a preg_replace to remove the -#?????? so the url looks like: http://www.something.com/section/filter/colour/red/size/small/
So i tried:
$string = $theURL;
$pattern = '/-\#(.*)\//i';
$replacement = '/';
$newURL = preg_replace($pattern, $replacement, $string);
That sort of works but it doesnt stop. If I have anything after the -#?????? it also removes that as well. But I thought having the / on the end would stop it doing that?
Hoping someone can help and thanks for reading
Upvotes: 1
Views: 138
Reputation: 31653
(.*)
pattern is gready, which means it'll match as many characters as possible. To match everything to the first slash use (.*?)
:
$pattern = '/-\#(.*?)\//i';
Upvotes: 1
Reputation: 10529
You need to use non-greedy quantifier.
$pattern = '/-\#(.*?)\//i';
Your regex is greedy, which means that (.*)\/
looks for the last slash, not the first one.
Upvotes: 1
Reputation: 91942
PCRE is greedy by default, meaning that .*
will match as big a chunk as possible. Make it ungreedy by adding the U
flag (for the entire pattern) or use .*?
(for just that wildcard part):
/-\#(.*)\//iU
or
/-\#(.*?)\//i
Upvotes: 1