kickstart Posted July 10, 2012 Share Posted July 10, 2012 Hi Trying to work out how to do this, but I am very weak at regular expressions. If I have a string as follows:- The quick "brown fox" jumped over the "lazy dog" I would like to extract the quotes strings (ie brown fox and lazy dog) Think I can do this with the following:- preg_match_all( '/".*?"/', 'The quick "brown fox" jumped over the "lazy dog"', $matches ) which will put each of the quoted strings into an array. However I would also like a way to extract the rest of the string separately - preferably without looping round the extracted array. Also, if I wanted to cope with strings enclosed in single quotes as well as double quotes, how can I expand it to cover that? Thank you All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/ Share on other sites More sharing options...
ignace Posted July 10, 2012 Share Posted July 10, 2012 This is possibly done better and faster without regex. Regex would require you to regex twice. Once for everything between " and once for everything that is not between ". Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/#findComment-1360490 Share on other sites More sharing options...
Barand Posted July 10, 2012 Share Posted July 10, 2012 non-regex solution <?php $str = 'The quick "brown fox" jumps over the "lazy dog".'; $result = xQuotes($str); echo '<pre>'.print_r($result, 1).'</pre>'; function xQuotes($str) { $k = strlen($str); $i = $j = 0; $res = array(); $inquotes = false; for ($i=0; $i<$k; ++$i) { switch($c = $str[$i]) { case '"': if (!$inquotes) ++$j; $inquotes = !$inquotes; break; default: $index = $inquotes ? $j : 0; if (isset($res[$index])) $res[$index] .= $c; else $res[$index] = $c; break; } } return $res; } ?> results==> Array ( 0 => The quick jumps over the . 1 => brown fox 2 => lazy dog ) Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/#findComment-1360491 Share on other sites More sharing options...
kickstart Posted July 10, 2012 Author Share Posted July 10, 2012 Hi Cheers. I thought a regex would have been the quickest, but it is a non trivial one it would seem. I will have a play with Barands solution, but modify it to cope with single quote delimited strings as well as double quote delimited strings. As an aside '/(".*?")|(\'.*?\')/' works to extract single and double quoted strings (just not found a way to extract things other than those). Thank you All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/#findComment-1360493 Share on other sites More sharing options...
kickstart Posted July 10, 2012 Author Share Posted July 10, 2012 Hi Played with Barands script to cope with both single and double quoted strings. function xQuotes($str) { $str_array = str_split($str); $j = 0; $res = array(); $insinglequotes = false; $indoublequotes = false; foreach($str_array AS $str_item) { switch(true) { case $str_item == "'" AND !$indoublequotes: if (!$insinglequotes) ++$j; $insinglequotes = !$insinglequotes; break; case $str_item == '"' AND !$insinglequotes: if (!$indoublequotes) ++$j; $indoublequotes = !$indoublequotes; break; default: $index = (($insinglequotes || $indoublequotes) ? $j : 0); if (isset($res[$index])) $res[$index] .= $str_item; else $res[$index] = $str_item; break; } } if ($insinglequotes || $indoublequotes) { $res[0] .= $res[$j]; unset($res[$j]); } return $res; } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/#findComment-1360505 Share on other sites More sharing options...
ignace Posted July 10, 2012 Share Posted July 10, 2012 Is the double case really necessary? case ($str_item === '"' || $str_item === "'") && !$indoublequotes: Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/#findComment-1360522 Share on other sites More sharing options...
kickstart Posted July 10, 2012 Author Share Posted July 10, 2012 Hi Afraid I think it is, to cope with entries delimited by either double quotes or single quotes, and to ignore any single quotes within double quotes. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/265465-extracting-quotes-with-regular-expressions/#findComment-1360525 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.