从 PHP 中的字符串开头和结尾删除引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9734758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove quotes from start and end of string in PHP
提问by newbie
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 中执行此操作 - 是否有此功能或我必须自己编写代码?
回答by Your Common Sense
回答by Steve Chambers
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('/^(\'(.*)\'|"(.*)")$/', '', $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
'"'"@";'"*&^*'' => "'"@";'"*&^*'
回答by user783322
trim
will remove all instances of the char from the start and end if it matches the pattern you provide, so:
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);
回答by Scott Horsley HRabbit
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.
这只会删除第一个和最后一个引号,它不会重复删除额外的内容,也不关心匹配的结尾。
回答by Mark Bradley
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 变量,因此是“某物”或“某物”,而不是“混合引号”。此外,匹配引号之间的任何内容都是保持完整。
路线 1 - 使用正则表达式
function sq_re($i) {
return preg_replace( '#^(\'|")(.*)$#isu', '', $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
路线 2 - 使用 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;
}
回答by sakhunzai
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('/^\'?(.*?(?=\'?$))\'?$/','' ,$singleQuotedString);
Output
输出
Hello this 'someword' and "somewrod" stas's SO
回答by Purpletoucan
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 检查字符串的第一个字符和最后一个字符是否是引号,如果是,则截断字符串。
回答by Melo Waste
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
注释有助于清晰......在字符串上类似数组的用法中的括号是可能的,并且比等效方法需要更少的处理工作,太糟糕了没有长度变量或最后一个字符索引