ess14 Posted April 9, 2008 Share Posted April 9, 2008 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); Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 9, 2008 Share Posted April 9, 2008 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'; Quote Link to comment Share on other sites More sharing options...
ess14 Posted April 9, 2008 Author Share Posted April 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 9, 2008 Share Posted April 9, 2008 So as few times as possible? Quote Link to comment Share on other sites More sharing options...
ess14 Posted April 9, 2008 Author Share Posted April 9, 2008 yes, few Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted April 9, 2008 Share Posted April 9, 2008 As far as I can see, text matching ([^\.)]+) does not exist between the start and the end. If ([^\.)]+) should work, neither a . or a ) could be present between the start and the end, but there is a . 13 lambs. mary Quote Link to comment Share on other sites More sharing options...
ess14 Posted April 9, 2008 Author Share Posted April 9, 2008 decided on this: preg_match('/' . preg_quote($start, '/') . '(.*)'. preg_quote($end, '/') .'/i', $string, $m); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.