mentalist Posted August 4, 2015 Share Posted August 4, 2015 Hi, this works with double quotes but fails on single quotes... I've been stuck on the wrong issue for a while so my heads boxed now lol Any help much appreciated. //$s='Test string [blog "xx yy" zz] one two three'; $s="Test string [blog 'xx yy' zz] one two three"; echo do_incode($s); function do_incode($s){ $a=array("blog"); foreach($a as $e){ $re="/\[".$e."(.+?)\]/"; //$re='/\['.$e.'"(?:\\\\.|[^\\\\"])*"|\S+\]/'; //$re='/\['.$e.' (`"([^"]*)"`)\]/'; if(preg_match_all($re, $s, $matches, PREG_OFFSET_CAPTURE)){ //$func=$this->api_register_get($e,'incode'); $n=0; foreach($matches[0] as $ee){ $va=str_getcsv(trim($matches[1][$n][0])," "); //$replace=$func($va); $replace=blog_incode($va); $re="/".preg_quote($ee[0])."/"; $s=preg_replace($re,$replace,$s,1); // USE 1 TO LIMIT TO FIRST MATCH, FOR DUPLICATES...??? $n++; } } } return $s; } function blog_incode($v){ //$s="<b>*** INCODE [ :".$v[0].": ".(isset($v[1])?$v[1]:"")." ] WORKED ***</b>"; $s="<b>*** INCODE [ :".$v[0].": ] WORKED ***</b>"; return $s; } Quote Link to comment https://forums.phpfreaks.com/topic/297627-split-words-and-quoted-strings/ Share on other sites More sharing options...
Solution requinix Posted August 4, 2015 Solution Share Posted August 4, 2015 Could it have something to do with the fact that you're trying to use CSV parsing on data that is not CSV? Should your regular expression be more specific, to match "blog" and "xx yy" and "zz" separately? Quote Link to comment https://forums.phpfreaks.com/topic/297627-split-words-and-quoted-strings/#findComment-1518003 Share on other sites More sharing options...
mentalist Posted August 4, 2015 Author Share Posted August 4, 2015 In reality it can of arbitrary length and either quoted or not, or even nothing at all. It does work as required when " are used, its just the ' case which doesn't......Oops forgot that was the reason it was extracting the strings... no wonder the regex wasn't acting as expectedI'll be back ... thankyou Quote Link to comment https://forums.phpfreaks.com/topic/297627-split-words-and-quoted-strings/#findComment-1518007 Share on other sites More sharing options...
mentalist Posted August 4, 2015 Author Share Posted August 4, 2015 Burgers got in the way yum yum function do_incode($s){ $a=array("blog"); foreach($a as $e){ $re="/\[".$e."(.+?)\]/"; if(preg_match_all($re, $s, $matches, PREG_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY)){ $n=0; foreach($matches[0] as $ee){ //$va=str_getcsv(trim($matches[1][$n][0])," "); $re='/"(.*?)"|(=)|\'(.*?)\'| +/'; $va=preg_split($re, trim($matches[0][$n][0],"[]"), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $replace=blog_incode($va); $re="/".preg_quote($ee[0])."/"; $s=preg_replace($re,$replace,$s,1); // USE 1 TO LIMIT TO FIRST MATCH, FOR DUPLICATES...??? $n++; } } } return $s; } Good enough for now... Cheers for pointing out my daftness lol Quote Link to comment https://forums.phpfreaks.com/topic/297627-split-words-and-quoted-strings/#findComment-1518012 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.