在PHP中从字符串的开头和结尾删除引号

时间:2022-03-12 05:44:54

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?

如何在PHP中实现这一点?是有函数还是我必须自己编写?

8 个解决方案

#1


67  

trim($string,'"');

here is the source

这是源

#2


9  

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

我也有类似的需要,写了一个函数,可以从字符串中删除前引和后引单引号或双引号:

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

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
'"'"@";'"*&^*'' => "'"@";'"*&^*'

#3


7  

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

如果char与您提供的模式匹配,trim将从开始和结束处删除所有实例,因此:

$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);

#4


2  

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. :)

尽管这条线早就该被消灭了,我还是忍不住用我称之为最简单的答案来回答。我注意到这条线在17日再次出现,所以我不觉得有那么糟糕。:)

Using samples as provided by Steve Chambers;

使用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.

这只会删除第一个和最后一个引用,它不会重复删除额外的内容,也不关心匹配的结束。

#5


0  

You need to use regular expressions, look at:-

你需要使用正则表达式,看看:-。

http://php.net/manual/en/function.preg-replace.php

http://php.net/manual/en/function.preg-replace.php

Or you could, in this instance, use substr to check if the first and then the last character of the string is a quote mark, if it is, truncate the string.

或者,在本例中,可以使用substr检查字符串的第一个和最后一个字符是否为引号,如果是,则截断字符串。

http://php.net/manual/en/function.substr.php

http://php.net/manual/en/function.substr.php

#6


0  

How about regex

正则表达式如何

//$singleQuotedString="'Hello this 'someword' and \"somewrod\" stas's SO";
//$singleQuotedString="Hello this 'someword' and \"somewrod\" stas's SO'";
$singleQuotedString="'Hello this 'someword' and \"somewrod\" stas's SO'";

$quotesFreeString=preg_replace('/^\'?(.*?(?=\'?$))\'?$/','$1' ,$singleQuotedString);

Output

输出

Hello this 'someword' and "somewrod" stas's SO   

#7


0  

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

这句话有助于澄清……在字符串上使用类似于数组的方括号是可能的,并且与等效方法相比需要更少的处理工作,糟糕的是没有长度变量或最后的字符索引

#8


0  

This is an old post, but just to cater for multibyte strings, there are at least two possible routes one can follow. I am assuming that the quote stripping is being done because the quote is being considered like a program / INI variable and thus is EITHER "something" or 'somethingelse' but NOT "mixed quotes'. Also, ANYTHING between the matched quotes is to be retained intact.
Route 1 - using a Regex

这是一个旧的帖子,但是为了满足多字节字符串,至少有两种可能的路径可以遵循。我假设引号已经被删除,因为引号被认为是一个程序/ INI变量,因此要么是“某物”,要么是“某物”,但不是“混合引号”。而且,匹配的引号之间的任何内容都要保持完整。使用Regex

function sq_re($i) {
    return preg_replace( '#^(\'|")(.*)\1$#isu', '$2', $i );
}

This uses \1 to match the same type quote that matched at the beginning. the u modifier, makes it UTF8 capable (okay, not fully multibyte supporting)

这使用\1来匹配与开始匹配的相同类型的引号。u修改器,使它能够支持UTF8(好的,不支持完全多字节)


Route 2 - using mb_* functions

使用mb_*函数

function sq($i) {
    $f = mb_substr($i, 0, 1);
    $l = mb_substr($i, -1);
    if (($f == $l) && (($f == '"') || ($f == '\'')) ) $i = mb_substr($i, 1, mb_strlen( $i ) - 2);
    return $i;
}

#1


67  

trim($string,'"');

here is the source

这是源

#2


9  

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

我也有类似的需要,写了一个函数,可以从字符串中删除前引和后引单引号或双引号:

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

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
'"'"@";'"*&^*'' => "'"@";'"*&^*'

#3


7  

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

如果char与您提供的模式匹配,trim将从开始和结束处删除所有实例,因此:

$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);

#4


2  

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. :)

尽管这条线早就该被消灭了,我还是忍不住用我称之为最简单的答案来回答。我注意到这条线在17日再次出现,所以我不觉得有那么糟糕。:)

Using samples as provided by Steve Chambers;

使用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.

这只会删除第一个和最后一个引用,它不会重复删除额外的内容,也不关心匹配的结束。

#5


0  

You need to use regular expressions, look at:-

你需要使用正则表达式,看看:-。

http://php.net/manual/en/function.preg-replace.php

http://php.net/manual/en/function.preg-replace.php

Or you could, in this instance, use substr to check if the first and then the last character of the string is a quote mark, if it is, truncate the string.

或者,在本例中,可以使用substr检查字符串的第一个和最后一个字符是否为引号,如果是,则截断字符串。

http://php.net/manual/en/function.substr.php

http://php.net/manual/en/function.substr.php

#6


0  

How about regex

正则表达式如何

//$singleQuotedString="'Hello this 'someword' and \"somewrod\" stas's SO";
//$singleQuotedString="Hello this 'someword' and \"somewrod\" stas's SO'";
$singleQuotedString="'Hello this 'someword' and \"somewrod\" stas's SO'";

$quotesFreeString=preg_replace('/^\'?(.*?(?=\'?$))\'?$/','$1' ,$singleQuotedString);

Output

输出

Hello this 'someword' and "somewrod" stas's SO   

#7


0  

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

这句话有助于澄清……在字符串上使用类似于数组的方括号是可能的,并且与等效方法相比需要更少的处理工作,糟糕的是没有长度变量或最后的字符索引

#8


0  

This is an old post, but just to cater for multibyte strings, there are at least two possible routes one can follow. I am assuming that the quote stripping is being done because the quote is being considered like a program / INI variable and thus is EITHER "something" or 'somethingelse' but NOT "mixed quotes'. Also, ANYTHING between the matched quotes is to be retained intact.
Route 1 - using a Regex

这是一个旧的帖子,但是为了满足多字节字符串,至少有两种可能的路径可以遵循。我假设引号已经被删除,因为引号被认为是一个程序/ INI变量,因此要么是“某物”,要么是“某物”,但不是“混合引号”。而且,匹配的引号之间的任何内容都要保持完整。使用Regex

function sq_re($i) {
    return preg_replace( '#^(\'|")(.*)\1$#isu', '$2', $i );
}

This uses \1 to match the same type quote that matched at the beginning. the u modifier, makes it UTF8 capable (okay, not fully multibyte supporting)

这使用\1来匹配与开始匹配的相同类型的引号。u修改器,使它能够支持UTF8(好的,不支持完全多字节)


Route 2 - using mb_* functions

使用mb_*函数

function sq($i) {
    $f = mb_substr($i, 0, 1);
    $l = mb_substr($i, -1);
    if (($f == $l) && (($f == '"') || ($f == '\'')) ) $i = mb_substr($i, 1, mb_strlen( $i ) - 2);
    return $i;
}