Re: [PHP] Re: Regex for extracting quoted strings
On 03/05/2011 04:38 PM, Mark Kelly wrote: Hi. Thanks for all the replies. On Saturday 05 Mar 2011 at 22:11 Simon J Welsh wrote: On 6/03/2011, at 11:08 AM, Shawn McKenzie wrote: $regex = '/([^]+)/'; Shawn, this regex gets me two copies of each string - one with and one without the double quotes - as did the one Nathan posted earlier. Also, you'll want preg_match_all rather than preg_match. Yeah, I realised that quite early on in my messing about. What I have ended up with is: $regex = '/.*?/'; $found = preg_match_all($regex, $sentence, $phrases); This still leaves the quotes in the phrases, but at least I only get one copy of each phrase. I'm just trimming the quotes afterwards. Thanks for all the advice. Mark $sentence = 'Dave said This is it. Nope, that is the wrong colour she replied.'; $regex = '/([^]+)/'; preg_match_all($regex, $sentence, $phrases); print_r($phrases[1]); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Regex for extracting quoted strings
Mark Kelly wrote: Hi. I'm hoping someone can help me extract text between double quotes from a string. $regex = 'some magic'; $r = preg_match($regex, $sentence, $phrases); So, if $sentence = 'Dave said This is it. Nope, that is the wrong colour she replied.'; I want $phrases to contain 'This is it' and 'Nope, that is the wrong colour'. Can anyone help? $regex = '/(.*)/imU'; $r = preg_match_all($regex, $sentence, $phrases); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Regex for extracting quoted strings
On 03/05/2011 09:26 AM, Mark Kelly wrote: Hi. I'm hoping someone can help me extract text between double quotes from a string. $regex = 'some magic'; $r = preg_match($regex, $sentence, $phrases); So, if $sentence = 'Dave said This is it. Nope, that is the wrong colour she replied.'; I want $phrases to contain 'This is it' and 'Nope, that is the wrong colour'. Can anyone help? Cheers, Mark $regex = '/([^]+)/'; -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Regex for extracting quoted strings
On 6/03/2011, at 11:08 AM, Shawn McKenzie wrote: On 03/05/2011 09:26 AM, Mark Kelly wrote: Hi. I'm hoping someone can help me extract text between double quotes from a string. $regex = 'some magic'; $r = preg_match($regex, $sentence, $phrases); So, if $sentence = 'Dave said This is it. Nope, that is the wrong colour she replied.'; I want $phrases to contain 'This is it' and 'Nope, that is the wrong colour'. Can anyone help? Cheers, Mark $regex = '/([^]+)/'; -- Thanks! -Shawn http://www.spidean.com Also, you'll want preg_match_all rather than preg_match. --- Simon Welsh Admin of http://simon.geek.nz/ Who said Microsoft never created a bug-free program? The blue screen never, ever crashes! http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Regex for extracting quoted strings
Hi. Thanks for all the replies. On Saturday 05 Mar 2011 at 22:11 Simon J Welsh wrote: On 6/03/2011, at 11:08 AM, Shawn McKenzie wrote: $regex = '/([^]+)/'; Shawn, this regex gets me two copies of each string - one with and one without the double quotes - as did the one Nathan posted earlier. Also, you'll want preg_match_all rather than preg_match. Yeah, I realised that quite early on in my messing about. What I have ended up with is: $regex = '/.*?/'; $found = preg_match_all($regex, $sentence, $phrases); This still leaves the quotes in the phrases, but at least I only get one copy of each phrase. I'm just trimming the quotes afterwards. Thanks for all the advice. Mark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Regex for extracting quoted strings
Maybe this will help. $regex = '/(?=)[^.]*(?=)/'; $r = preg_match_all($regex, $sentence, $phrases);