Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: preg_match help

  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question preg_match help

    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.

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    This worked for me.

    Code:
    <?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;
    ?>
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It won't work qas desired.
    Consider the string:
    Code:
    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()
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try this and see how it goes
    Code:
    preg_match('/(?<=")[^ ][^"]*and[^"]*[^ ](?=")/is', $paragraph, $matched);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •