Function to extract matching terms from 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
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Function to extract matching terms from a string

Post by Randwulf »

Is there a function to extract matching terms (terms that match a regex pattern) from a string and into an array?

For example, if I had a string that said "aba.....bbb......ccc.....bba.....ada" and I had the regex pattern "/a[a-z]a/", I would want to get an array with two entries, "aba" and "ada". I searched for such a function in the API but couldn't find one. Is there any sort of function that will provide this sort of functionality? It would make my life a whole lot easier, although I can do what needs to be done with a lot of replace()s and explode()s.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Function to extract matching terms from a string

Post by John Cartwright »

Code: Select all

preg_match_all()
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: Function to extract matching terms from a string

Post by Randwulf »

Ahhh, yes. Silly me, I didn't think before to check the parameter list for extra return values.

Thanks
Post Reply