The Little Guy Posted September 17, 2008 Share Posted September 17, 2008 I need to find text between: q= and an & from a url passed to a variable. but... sometimes there is no ampersand, because the q= is at the end of the URL, so How can I find the q string? if(preg_match("~q=(.+?)&~",$ref,$matches)){ $search = $matches[1]; } my code receives this string as ONE string NOT multiple GET variables Link to comment https://forums.phpfreaks.com/topic/124712-solved-find-q-string-in-a-url/ Share on other sites More sharing options...
DarkWater Posted September 17, 2008 Share Posted September 17, 2008 <?php $ref = "someurl.php?foobar=lol&q=ROFL"; preg_match('/q=(.+?)(?:&|$)/i', $ref, $matches); print_r($matches); Link to comment https://forums.phpfreaks.com/topic/124712-solved-find-q-string-in-a-url/#findComment-644163 Share on other sites More sharing options...
The Little Guy Posted September 17, 2008 Author Share Posted September 17, 2008 works! Link to comment https://forums.phpfreaks.com/topic/124712-solved-find-q-string-in-a-url/#findComment-644253 Share on other sites More sharing options...
effigy Posted September 18, 2008 Share Posted September 18, 2008 ~(?<=q=)[^&]*~ Link to comment https://forums.phpfreaks.com/topic/124712-solved-find-q-string-in-a-url/#findComment-644844 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.