Re: Regular Expression just one step away from what I need....
| From: | Thijs Lensselink | Date: | Fri, 17 Aug 2007 17:55:09 +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-260974@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
> (
> [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!
>
>
If it's only real words this will do:
$theString = "'foo''bar''glorp'";
preg_match_all( "/'([a-z]+)'/Ui", $theString, $matches);