[英]Php extract a string between double quotes
I need to extract a string with any symbols between a doublequotes using preg_match including double quotes as well. 我需要使用preg_match以及双引号来提取双引号之间包含任何符号的字符串。
I've tried all solutions in the question below but nothing has worked for my case: php to extract a string from double quote 我在下面的问题中尝试了所有解决方案,但对于我的情况没有任何作用: php从双引号中提取字符串
Sample string: "ASD ""ASD ADS""" 示例字符串: “ ASD”“ ASD ADS”“”
I need to extract: ASD ""ASD ADS"" 我需要提取:ASD“” ASD ADS“”
Current code which is working except I don't know how to handle the exception above which ruining whole structure: 当前的代码是有效的,除了我不知道如何处理破坏整个结构的异常:
$regex = '/"(.*)"/imU';
$content = file_get_contents($file->getRealPath());
$filename = $file->getClientOriginalName();
preg_match_all($regex, $content, $matches);
return $matches[0];
To serve properly 2 adjacent double quotes between the opening and closing double quote, you must use 2 alternatives: either a char other than the double quote or 2 consecutive double quotes. 要在开始和结束双引号之间正确地使用2个相邻的双引号,您必须使用2个替代方法:双引号以外的char或2个连续的双引号。
So the regex can be as follows: 因此正则表达式可以如下所示:
/"(?:[^"]|"")+"/g
Description: 描述:
"
- Match the "opening" double quote. "
-匹配“开头”双引号。 (?:
- Start of a non-capturing group, needed due to the +
quantifier after it. (?:
-一个非捕获组的开始,由于后面有+
量,所以需要。
[^"]
- The first alternative - any char other than the double quote. [^"]
-第一种选择-除双引号外的任何字符。 |
- Or. - 要么。
""
- Two double quotes. ""
-两个双引号。 )
- End of the non-capturing group. )
-非捕获组的结尾。 +
- This group can occur 1 or more times. +
-该组可以出现1次或多次。 "
- Match the "closing" double quote. "
-匹配“结束”双引号。 It is enough to use g
option only. 仅使用g
选项就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.