...

View Full Version : preg_match help



komodo
11-04-2006, 06:44 PM
Regex has always been a pain for me, but for some reason I'm having unusal difficulty with this one.

I need to find the entire content between either two double-quotes OR two single-quotes that contain a certain word inside a large document.

For example, if I have a paragraph like this:


Among dog lovers, "dogs are generally valued" for "their intelligence", and both "anecdotal evidence and scientific" research suggest that dogs have a reasonably high intelligence. "This intelligence is" expressed "differently with different breeds" and individuals, however.

And I run a command like this:

preg_match("/[\"|'](.+?and.+?)[\"|']/i",$paragraph,$matched);

I know that's not right, since the pipes are being interpreted as a literal because of the square brackets.

I'm trying to return this:

anecdotal evidence and scientific
The only quoted phrase that includes "and".

I figured out one way that works:

/(\"|')(.*and.*)(\"|')/i

Except, if the word I'm looking for is at the very end of the phrase, directly before the quote, it reads right over the quote and keeps reading all the way until it hits the next quote.

With the .* though, it matches zero characters, right? So it shouldn't matter if there's no characters between the word and the quote?

I'm still working on it, but I'd appreciate any help. Thanks.

syosoft
11-05-2006, 03:30 AM
This worked for me.



<?php
$text = 'Among dog lovers, "dogs are generally valued" for "their intelligence", and both "anecdotal evidence and scientific" research suggest that dogs have a reasonably high intelligence. "This intelligence is" expressed "differently with different breeds" and individuals, however.';

function findQuotesIncluding($text='',$word='')
{
preg_match_all('/("|\')([\w\s]+\b'.$word.'\b[\w\s]+)("|\')/i',$text,$matches);
return sizeof($matches[0]) > 0 ? $matches[2] : array();
}

$matches = findQuotesIncluding($text,'and');

echo '<pre>';
print_r($matches);
exit;
?>

marek_mar
11-05-2006, 12:41 PM
It won't work qas desired.
Consider the string:

a string "any string is" what a string does "some string does not" and thats false
Will match the string between double quotes but not in quotes.
It fails to match words next to quotes, for example "any" won't be matched.
The regex doesn't account for punctuation. For example, words with a "." next to them won't match.
The $word should be escaped by preg_quote() (http://www.php.net/preg_quote)

Nicklas
11-06-2006, 01:39 AM
Try this and see how it goes

preg_match('/(?<=")[^ ][^"]*and[^"]*[^ ](?=")/is', $paragraph, $matched);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum