Re: Regular Expression just one step away from what I need....
| From: | M. Sokolewicz | Date: | Fri, 17 Aug 2007 17:38:14 +0000 |
| Subject: | Re: Regular Expression just one step away from what I need.... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-260973@lists.php.net to get a copy of this message | ||
Jay Blanchard wrote:
Given the string 'foo''bar''glorp' (all quotes are single quotes)I had hoped to find a regular expression using preg_match that would return an array containing just those words without having to go through additional gyrations, like exploding a string to get an array. I have only had limited luck $theString = "'foo''bar''glorp'"; preg_match( "/\'(.*)\'/", $theString, $matches); print_r($matches); Array (2(-3) steps: 1. Make it _un_greedy (preg_* are greedy by default, so "/'(.*)'/U" will match foo''bar''glorp, making it ungreedy (/U modifier) would make it match just foo 2. Make it fetch _all_ matches, use preg_match_all (3. there's no need to escape single quotes in this string :)) The following works: preg_match_all("/'(.*)'/U", "'foo''bar''glorp'", $matches); print_r($matches);[0] => 'foo''bar''glorp' [1] => foo''bar''glorp) Of course $matches[0] has the entire string and $matches[1] contains the returned string minus the leading and trailing single quote. I can explode $matches[1] to get what I want, but it is an extra step and I am sure that I have done this before but cannot locate the code in question. I would like the results to be Array ([0] => 'foo''bar''glorp' [1] => foo [2] => bar [3] => glorp) That way I can use the $matches array without having to create another array to hold the values. I feel as if I am really close on the regex to do this, but cannot seem to find (after much head scratching and teeth gnashing) the proper solution. Much thanks!