Remove quotes from start and end of string in PHP

前端 未结 8 1850
醉酒成梦
醉酒成梦 2020-12-29 01:34

I have strings like these:

\"my value1\" => my value1
\"my Value2\" => my Value2
myvalue3 => myvalue3 
         


        
8条回答
  •  借酒劲吻你
    2020-12-29 02:19

    If you like performance over clarity this is the way:

    // Remove double quotes at beginning and/or end of output
    $len=strlen($output);
    if($output[0]==='"') $iniidx=1; else $iniidx=0;
    if($output[$len-1]==='"') $endidx=-1; else $endidx=$len-1;
    if($iniidx==1 || $endidx==-1) $output=substr($output,$iniidx,$endidx);
    

    The comment helps with clarity... brackets in an array-like usage on strings is possible and demands less processing effort than equivalent methods, too bad there isnt a length variable or a last char index

提交回复
热议问题