Jump to content

[SOLVED] regular expression


ess14

Recommended Posts

having a little trouble with this expression....im very new to regular expressions, so its no wonder im having trouble.

As you can see (when u run this script) im getting no output.

the problem seems to be the fullstop (.)

how do i get the array to return the word 'lambs.'?

even when i try escape the full stop, its like the stop is still stuffing things up...what am i missing?

 

function findinside($start, $end, $string) {
        preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
        return $m[1];
    }
    
    $start = "13";
    $end = "mary";
    $string = "mary has 6 lambs. phil has 13 lambs. mary stole phil's lambs. now mary has all the lambs.";

    $out = findinside($start, $end, $string);

    print_r ($out);

Link to comment
Share on other sites

Wrong forum... but do you want as many results as possible, or as few? IE

 

$start = 'mary';

$end = 'lambs';

 

$string = 'there are 5 lambs. mary has 3 lambs and john has 2 lambs';

or

$string = 'there are 5 lambs. mary has 3 lambs and john has 2 lambs';

Link to comment
Share on other sites

in my usage it should only have 1 result. ie there will be no more matches.

if u look at the above code the start and end can only leave 1 thing. but how do i get that into the array?

the full stop is stuffing it up somehow

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.