Remove quotes from start and end of string in PHP

前端 未结 8 1853
醉酒成梦
醉酒成梦 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:08

    As much as this thread should have been killed long ago, I couldn't help but respond with what I would call the simplest answer of all. I noticed this thread re-emerging on the 17th so I don't feel quite as bad about this. :)

    Using samples as provided by Steve Chambers;

    echo preg_replace('/(^[\"\']|[\"\']$)/', '', $input);
    

    Output below;

    Input text         Output text
    --------------------------------
    No quotes       => No quotes
    "Double quoted" => Double quoted
    'Single quoted' => Single quoted
    "One of each'   => One of each
    "Multi""quotes" => Multi""quotes
    '"'"@";'"*&^*'' => "'"@";'"*&^*'
    

    This only ever removes the first and last quote, it doesn't repeat to remove extra content and doesn't care about matching ends.

提交回复
热议问题