preg_??? Need to extract a PORTION of a string...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tasteful
Forum Newbie
Posts: 1
Joined: Thu Sep 25, 2008 1:26 pm

preg_??? Need to extract a PORTION of a string...

Post 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?
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: preg_??? Need to extract a PORTION of a string...

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: preg_??? Need to extract a PORTION of a string...

Post 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.
Post Reply