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 #178583 is a reply to message #178582] Mon, 09 July 2012 12:44 Go to previous messageGo to previous message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma:
Senior Member
On Jul 9, 9:50 am, Jason C <jwcarl...@gmail.com> wrote:
> I'm working with a message board database that already has a bunch of YouTube links in the comments, and I'm trying to replace all of the links with a new alternate.
>
> The existing strings are like:
>
> $this_comment = '<a href="" target="_new">http://www.youtube.com/watc...vidid</a><br><br><a href="" target="_new">http://www.youtube.com/watc...vidid_2</a>';
>
> Notice that this string has 2 separate YouTube links.
>
> If you're not familiar, YouTube has several possible link formats, so using parse_url() doesn't really work:
>
> youtube.com/v/{vidid}
> youtube.com/vi/{vidid}
> youtube.com/?v={vidid}
> youtube.com/?vi={vidid}
> youtube.com/watch?v={vidid}
> youtube.com/watch?vi={vidid}
> youtu.be/{vidid}
> youtube.com/v/{vidid}?feature=autoshare&version=3&autohide=1&au toplay=1
>
> I've written the regex to find the ID and replace the link correctly, but it only works with the first link that it finds. How do I make it work with all of the matching links in the string?
>
> Here's what I have:
>
> // Fetch the VIDID
> $this_id = preg_replace("#.*?<a href=\" http://.*?youtu\.*?be[\.com]*/[watch]*[\?]*(v/|v=|vi/|vi=)*(.*?)[&.+]*\ " target=\"_new\">.*?<\/a>.*#",
>   "$2", $this_comment);
>
> // I'm not sure why preg_replace isn't catching the extra variables;
> // I thought the [&.+]* would do this? Either way, this is a workaround:
> list($this_id) = explode("&", $this_id);
>
> // Replace link
> if ($this_id) {
>   $new_link = "Example replacement: $this_id";
>
>   $this_comment = preg_replace("#<a href=\"http://.*?youtu\.*?be[\.com]*/[watch]*[\?]*(v/|v=|vi/|vi=)*" . $this_id . "[&.+]*\" target=\"_new\">.*?<\/a>#",
>     "$new_link", $this_comment);

Well I'm very confused by all this. First of all, why are you using
preg_replace to extract the vidid? I would have thought that a job
better suited to preg_match.

Next, in your string assigned to $this_comment, the first vidid is
different to the other 2, so why are you expecting $this_id to match
all of them?
[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: Fri Nov 29 22:32:33 GMT 2024

Total time taken to generate the page: 0.04293 seconds