Page 1 of 1
preg_??? Need to extract a PORTION of a string...
Posted: Thu Sep 25, 2008 1:30 pm
by tasteful
I've been looking all throughout the PREG functions, and similar ones...
I can't find what I need.
I have a string that contains something along the lines of this:
?abcd=1234&efgh=5678&ijkl=901&mno=234&pqr=567890
I need just ONE of those values, so I need to extract everything between "mno=" and the following "&"
Which would result in "234"
What function would best do this for me?
Re: preg_??? Need to extract a PORTION of a string...
Posted: Thu Sep 25, 2008 3:50 pm
by The_Anomaly
I assume that using $_GET['mno'] wouldn't work--even though that's clearly a querystring?
I'm no master on the preg_* functions, although I"m pretty certain it has to be in there. Maybe try approaching it with the explode() function, using the & as the delimiter, and then just stripping off the 'mno.'
I'm sure there are cleaner ways of doing it, so by all means keep looking, but I bet the above would work too.
Re: preg_??? Need to extract a PORTION of a string...
Posted: Thu Sep 25, 2008 6:01 pm
by Stryks
Just out of interest ... where are you getting that string? Just making sure it isn't from the current page URL, in which case .. well, $_GET is your man.
But assuming it's not a simple oversight, you could easily go with something similar to the suggestion from The_Anomoly. Or you could just say ...
Code: Select all
parse_str($query_str, $query);
echo $query['mno'];
Of course, you might need to remove that ? ... or not ... I can't test it from here at the moment.
Either way, the preg functions would be overkill and add more overhead than it's worth.
Hope that helps anyhow.