Re: regex help
| From: | Monte Ohrt | Date: | Tue, 01 Aug 2000 13:39:50 +0000 |
| Subject: | Re: regex help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9431@lists.php.net to get a copy of this message | ||
Hi Kev,
kev wrote:
>
> Hi,
>
> This should be incredibly easy, but I can't get it to work - I'm trying
> to extract terms from a string, the terms being enclosed in quotes, eg
> '"michael owen" "gerrard houllier"'. So why doesn't the
> following code
> find any matches?
>
> $query = "\"michael owen\" \"gerrard houllier\"";
> $nmatches = preg_match_all( "/(\\\".*\\\")/", $query, $matches );
> echo "\$nmatches = ".$nmatches."<BR>";
>
> thanks,
>
> - Kev
It seems to work, you just need to get your results from $matches. Give
this a try, this regular expression is also a bit more efficient, and
not as greedy:
$query = "\"michael owen\" \"gerrard houllier\"";
if (preg_match_all( "/(\"[^\"]+\")/", $query, $matches ))
{
for($i=0; $i <= count($matches); $i++)
echo "matched: ".$matches[0][$i]."<BR>\n";
}
else
echo "nothing matched.\n";