On Mon, 2015-08-24 at 16:02 -0400, Ian Evans wrote: > On Sun, Aug 23, 2015 at 7:06 PM, Aziz Saleh <azizsaleh@xxxxxxxxx> wrote: > > > > > > > On Sun, Aug 23, 2015 at 6:59 PM, Ian Evans <dheianevans@xxxxxxxxx> wrote: > > > >> On Sun, Aug 23, 2015 at 6:52 PM, Aziz Saleh <azizsaleh@xxxxxxxxx> wrote: > >> > >> > > >> > > >> > On Sun, Aug 23, 2015 at 6:30 PM, Ian Evans <dheianevans@xxxxxxxxx> > >> wrote: > >> > > >> >> I've been looking at the regex functions. Either it's a lack of > >> caffeine > >> >> or > >> >> it's right in front of my nose, but how do I put part of a regex find > >> into > >> >> a variable? Plus I suck at regex. > >> >> > >> >> e.g. > >> >> > >> >> I'm interested in capturing the names of people who are found in text > >> >> surrounded by the following format: > >> >> > >> >> "John Doe":/cr/johndoe/ or "Jane Smith":/cr/janesmith/ > >> >> > >> >> I'd want to place John Doe or Jane Smith into the variable. The search > >> >> will > >> >> always be for "name":/cr/nameurl/ > >> >> > >> >> What regex and php function would I use? Thanks for any pointers. One > >> of > >> >> these days I'll master regex. > >> >> > >> > > >> > If the names will always contain double quotes, it should be as easy as: > >> > > >> > $var = ' "John Doe":/cr/johndoe/ or "Jane Smith":/cr/janesmith/'; > >> > preg_match_all('/".*"/U', $var, $res); > >> > print_r($res); > >> > > >> > U to make it ungreedy (its greedy by default). If there are other quotes > >> > in the text, you can add the colon to the expression as well. > >> > > >> > >> Thanks. There might be other quotes in the article, so if I include the > >> colon, it'd be before the /U? > >> > > > > Yeah, you might also want to do a select to avoid getting the actual > > quotes: > > > > preg_match_all('/"(.*)":/U', $var, $res); > > print_r($res[1]); > > > > Thanks so much, that worked like a charm. Just realized that it will grab > all links in the article. (These are PHPTextile formatted links). So if I > want to narrow it down to just the links with /cr/ is this the correct > escaped forma: > > preg_match_all('/"(.*)":\/cr\//U', $var, $res); I'd probably change the regex slightly as right now it will also match empty quotes. Change the (.*) to ([^"]+) That way, you shouldn't need to worry about it being greedy, as it can't match quotes. The plus is for one or more of the preceding match, instead of the zero or more that * gives. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php