Jump to content

Split words and quoted strings


mentalist

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.