Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove quotes from start and end of string in PHP

Tags:

string

php

I have strings like these:

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

I need to get rid of " (double-quotes) in end and start, if these exist, but if there is this kind of character inside String then it should be left there. Example:

"my " value1" => my " value1

How can I do this in PHP - is there function for this or do I have to code it myself?

like image 351
newbie Avatar asked Mar 16 '12 09:03

newbie


People also ask

How do I strip a quote from a string in PHP?

Try this: str_replace('"', "", $string); str_replace("'", "", $string); Otherwise, go for some regex, this will work for html quotes for example: preg_replace("/<!

How do you remove quotation marks from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you escape quotes in PHP?

In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.

How remove all special characters from a string in PHP?

Using str_ireplace() Method: The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).


3 Answers

The literal answer would be

trim($string,'"'); // double quotes
trim($string,'\'"'); // any combination of ' and "

It will remove all leading and trailing quotes from a string.

If you need to remove strictly the first and the last quote in case they exist, then it could be a regular expression like this

preg_replace('~^"?(.*?)"?$~', '$1', $string); // double quotes
preg_replace('~^[\'"]?(.*?)[\'"]?$~', '$1', $string); // either ' or " whichever is found

If you need to remove only in case the leading and trailing quote are strictly paired, then use the function from Steve Chambers' answer

However, if your goal is to read a value from a CSV file, fgetcsv is the only correct option. It will take care of all the edge cases, stripping the value enclosures as well.

like image 74
Your Common Sense Avatar answered Oct 17 '22 23:10

Your Common Sense


I had a similar need and wrote a function that will remove leading and trailing single or double quotes from a string:

/**
 * Remove the first and last quote from a quoted string of text
 *
 * @param mixed $text
 */
function stripQuotes($text) {
  return preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $text);
} 

This will produce the outputs listed 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
'"'"@";'"*&^*'' => "'"@";'"*&^*'

Regex demo (showing what is being matched/captured): https://regex101.com/r/3otW7H/1

like image 21
Steve Chambers Avatar answered Oct 17 '22 21:10

Steve Chambers


trim will remove all instances of the char from the start and end if it matches the pattern you provide, so:

$myValue => '"Hi"""""';
$myValue=trim($myValue, '"');

Will become:

$myValue => 'Hi'.

Here's a way to only remove the first and last char if they match:

$output=stripslashes(trim($myValue));

// if the first char is a " then remove it
if(strpos($output,'"')===0)$output=substr($output,1,(strlen($output)-1));

// if the last char is a " then remove it
if(strripos($output,'"')===(strlen($output)-1))$output=substr($output,0,-1);
like image 8
user783322 Avatar answered Oct 17 '22 22:10

user783322