Reputation: 258
I've tried searching for the answer but it's a tough question to ask in the first place, here goes.
Consider the following URL(s):
the following would be returned using the correct regular expression:
I'm trying to use regular expression to get the centre value(s) ONLY. I know the first and last words of the string in all cases i.e. 'hotels' (first) and '5star' (last).
UPDATE: I need to use regular expressions as the URL's are being routed through Codeigniters URI router. The centre part of the URI is dynamically built for search results.
Upvotes: 0
Views: 99
Reputation: 131841
For such simple cases you can use a explode()
$parts = explode('-', $string);
$wanted = array_slice($parts, 1, count($parts)-2);
Update: A regular expression. I still think it's easier to split the string into pieces manually.
~hotels-(.+)-5start~
Upvotes: 3
Reputation: 324620
Since you know the first and last words, you also know their lengths.
$out = substr($in,7,-6);
Or more generally:
$out = substr($in,strlen($begin),-strlen($end));
EDIT: If regex is required, just use /(?<=hotels-).*(?=-5star)/
Upvotes: 5