Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从双引号中提取字符串_Php_String_Preg Match_Preg Match All_Double Quotes - Fatal编程技术网

php从双引号中提取字符串

php从双引号中提取字符串,php,string,preg-match,preg-match-all,double-quotes,Php,String,Preg Match,Preg Match All,Double Quotes,我有一个字符串: 这是一段文字,“您的余额为0.10美元”,结束于0 如何提取双引号之间的字符串并仅包含文本(不包含双引号): 你的余额剩下0.10美元 我尝试了preg\u match\u all(),但没有成功。只需使用str\u replace并转义: str_replace("\"","",$yourString); 编辑: 对不起,我没有看到第二个引号后面有文字。在这种情况下,我只需进行两次搜索,一次搜索第一个引号,另一次搜索第二个引号,然后执行substr以增加这两个引号之间的所有

我有一个字符串:

这是一段文字,“您的余额为0.10美元”,结束于0

如何提取双引号之间的字符串并仅包含文本(不包含双引号):

你的余额剩下0.10美元


我尝试了
preg\u match\u all()
,但没有成功。

只需使用str\u replace并转义:

str_replace("\"","",$yourString);
编辑:


对不起,我没有看到第二个引号后面有文字。在这种情况下,我只需进行两次搜索,一次搜索第一个引号,另一次搜索第二个引号,然后执行substr以增加这两个引号之间的所有内容。

正则表达式
'([^\\“]+)”
将匹配两个双引号之间的任何内容

$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\\"]+)"', $string, $result);
echo $result[0];

只要格式保持不变,就可以使用正则表达式执行此操作。
“([^”]+)”
将匹配该模式

  • 双引号
  • 至少有一个非双引号
  • 双引号
[^”]+
周围的括号表示该部分将作为单独的组返回

<?php

$str  = 'This is a text, "Your Balance left $0.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 $0.10
试试这个:

preg_match_all('`"([^"]*)"`', $string, $results);

您应该在$results[1]中获取所有提取的字符串。

与其他答案不同,它支持转义,例如
“字符串中带\”引号”


对于寻找全功能字符串解析器的所有人,请尝试以下方法:

(?:(?:"(?:\\"|[^"])+")|(?:'(?:\\'|[^'])+'));
在预匹配中使用:

$haystack = "something else before 'Lars\' Teststring in quotes' something else after";
preg_match("/(?:(?:\"(?:\\\\\"|[^\"])+\")|(?:'(?:\\\'|[^'])+'))/is",$haystack,$match);
返回:

Array
(
    [0] => 'Lars\' Teststring in quotes'
)

这适用于单引号和双引号字符串片段。

也适用,但有没有办法将引号本身从返回的字符串中排除?您可能会发现,如中所示。它不是有效的正则表达式!
Array
(
    [0] => 'Lars\' Teststring in quotes'
)