Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #6509
| From | "Peter H. Coffin" <hellsop@ninehells.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Extracting multiple matches from a string using preg_replace |
| Date | 2012-07-09 08:29 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnjvln5b.h39.hellsop@nibelheim.ninehells.com> (permalink) |
| References | <b5a49bde-0022-4dbf-ab01-91e4ca3b56e8@googlegroups.com> |
On Mon, 9 Jul 2012 01:50:39 -0700 (PDT), Jason C 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="http://www.youtube.com/watch?v=vidid"
> target="_new">http://www.youtube.com/watc...vidid</a><br><br><a
> href="http://www.youtube.com/watch?v=vidid_2"
> 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&autoplay=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?
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.
> 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);
^^ -- enumerated result
>
> // 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);
Define "catching" in this context. If you want it back, you need to
paren-tag it so it goes into an enumerated output slot.
> // 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); }
--
"'I'm not sleeping with a jr. high schooler! I have a life-sized doll
that looks like one.' Uh huh. That sounds SO much less pathetic."
-- Piro's Conscience www.megatokyo.com
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Extracting multiple matches from a string using preg_replace Jason C <jwcarlton@gmail.com> - 2012-07-09 01:50 -0700
Re: Extracting multiple matches from a string using preg_replace Captain Paralytic <paul_lautman@yahoo.com> - 2012-07-09 05:44 -0700
Re: Extracting multiple matches from a string using preg_replace Jason C <jwcarlton@gmail.com> - 2012-07-09 11:58 -0700
Re: Extracting multiple matches from a string using preg_replace "Peter H. Coffin" <hellsop@ninehells.com> - 2012-07-09 08:29 -0500
Re: Extracting multiple matches from a string using preg_replace Jason C <jwcarlton@gmail.com> - 2012-07-09 12:00 -0700
Re: Extracting multiple matches from a string using preg_replace Jason C <jwcarlton@gmail.com> - 2012-07-10 02:08 -0700
csiph-web