php php从双引号中提取字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1017051/
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
php to extract a string from double quote
提问by conandor
I have a string:
我有一个字符串:
This is a text, "Your Balance left $0.10", End 0
这是一条文字,“您的余额还剩 0.10 美元”,结束 0
How can I extract the string in between the double quotes and have only the text (without the double quotes):
如何提取双引号之间的字符串并且只有文本(没有双引号):
Your Balance left $0.10
您的余额还剩 0.10 美元
I have tried preg_match_all()but with no luck.
我试过,preg_match_all()但没有运气。
回答by Tom Haigh
As long as the format stays the same you can do this using a regular expression. "([^"]+)"will match the pattern
只要格式保持不变,您就可以使用正则表达式执行此操作。"([^"]+)"将匹配模式
- Double-quote
- At least one non-double-quote
- Double-quote
- 双引号
- 至少一个非双引号
- 双引号
The brackets around the [^"]+means that that portion will be returned as a separate group.
周围的括号[^"]+表示该部分将作为一个单独的组返回。
<?php
$str = 'This is a text, "Your Balance left (?:(?:"(?:\"|[^"])+")|(?:'(?:\'|[^'])+'));
.10", End 0';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
//output: Your Balance left $haystack = "something else before 'Lars\' Teststring in quotes' something else after";
preg_match("/(?:(?:\"(?:\\\"|[^\"])+\")|(?:'(?:\\'|[^'])+'))/is",$haystack,$match);
.10
回答by user426486
For everyone hunting for a full featured string parser, try this:
对于正在寻找全功能字符串解析器的每个人,请尝试以下操作:
Array
(
[0] => 'Lars\' Teststring in quotes'
)
Use in preg_match:
在 preg_match 中使用:
preg_match_all('`"([^"]*)"`', $string, $results);
Returns:
返回:
$content = stripslashes(preg_match('/"((?:[^"]|\\.)*)"/'));
This works with single and double quoted string fragments.
这适用于单引号和双引号字符串片段。
回答by user426486
Try this :
尝试这个 :
$string = '"Your Balance left str_replace("\"","",$yourString);
.10", End 0';
preg_match('"([^\"]+)"', $string, $result);
echo $result[0];
You should get all your extracted strings in $results[1].
您应该在 $results[1] 中获取所有提取的字符串。
回答by Kornel
Unlike other answers, this supports escapes, e.g. "string with \" quote in it".
与其他答案不同,这支持转义,例如"string with \" quote in it".
回答by Rich Adams
The regular expression '"([^\\"]+)"'will match anything between two double quotes.
正则表达式'"([^\\"]+)"'将匹配两个双引号之间的任何内容。
回答by PaulJWilliams
Just use str_replace and escape the quote:
只需使用 str_replace 并转义引号:
##代码##Edit:
编辑:
Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.
抱歉,没有看到第二个引用后有文字。在这种情况下,我只需要进行 2 次搜索,一次搜索第一个引号,一次搜索第二个引号,然后执行 substr 以额外添加两者之间的所有内容。

