FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Extracting multiple matches from a string using preg_replace
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Extracting multiple matches from a string using preg_replace [message #178590 is a reply to message #178584] Tue, 10 July 2012 09:08 Go to previous message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma:
Member
On Monday, July 9, 2012 9:29:15 AM UTC-4, Peter H. Coffin wrote:
> That's the drawback to using preg_replace() for this. You can't capture
> all the bits you want to extract because you *must* enumerate them.
> preg_match_all() returns an array of matches, which is what you want if
> you don't know how many you're going to get back going in.

Just a note for anyone else reading this later, preg_match_all() did work perfectly. I changed:

$this_id = preg_match(...);

to this:

preg_match_all("#<a href=\" http://.*?youtu\.*?be[\.com]*/[watch]*[\?]*(v/|v=|vi/|vi=)*(.*?)[&.+]*\ " target=\"_new\">.*?<\/a>#m",
$this_comment, $matches);


This gives me a multidimensional array of $matches, where $matches[2] is the array that holds the values from $2.

So after finding the array, it's a simple matter of putting the second preg_replace() in a foreach loop:

foreach ($matches[2] as $this_id) {
// I'm still not sure why $this_id is keeping the other params
list($this_id) = explode("&", $this_id);

$this_comment = preg_replace(...);
}

Thanks for the help, Peter! If you happen to see the error I'm making with the extra params (forcing me to use explode to get rid of them), I'd appreciate any insight. The workaround is working, though, so it's not a big deal... just sloppy, I guess.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Invitacion a INFOSOFT 2012 [PUCP]
Next Topic: PHP does not flush output on IIS7 ..
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 03:51:43 GMT 2024

Total time taken to generate the page: 0.05032 seconds