Jump to content

Split words and quoted strings


Go to solution Solved by requinix,

Recommended Posts

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;
}
Link to comment
https://forums.phpfreaks.com/topic/297627-split-words-and-quoted-strings/
Share on other sites

  • Solution

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?

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 expected

I'll be back ::) ... thankyou

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.