我有一个包含双引号或单引号的字符串。我需要做的是在引用之间回应所有内容:
$str = "'abc de', xye, jhy, jjou";
$str2 = "\"abc de\", xye, jhy, jjou";
我不介意使用正则表达式(preg_match)或任何其他内置的函数。
请建议。
此致
答案 0 :(得分:6)
$str = "'abc de', xye, \"jhy\", blah blah 'bob' \"gfofgok\", jjou";
preg_match_all('/".*?"|\'.*?\'/', $str, $matches);
print_r($matches);
返回:
Array (
[0] => Array (
[0] => 'abc de'
[1] => "jhy"
[2] => 'bob'
[3] => "gfofgok"
)
)
正则表达式的解释:
" -> Match a double quote
.* -> Match zero or more of any character
?" -> Match as a non-greedy match until the next double quote
| -> or
\' -> Match a single quote
.* -> Match zero or more of any character
?\' -> Match as non-greedy match until the next single quote.
所以$matches[0]
是一个数组,包含单引号或双引号内的所有字符串。
答案 1 :(得分:1)
根据您的问题查看并尝试在使用之前了解它
$str = "'abc de', xye, jhy, jjou";
$str2 = "\"abc de\", xye, jhy, jjou";
$match = $match2 = array();
preg_match("/'(.+)'/", $str, $match);
preg_match("/\"(.+)\"/", $str2, $match2);
print_r($match);
print_r($match2);
答案 2 :(得分:0)
对于这种情况,您可以使用explode内置功能:
function getBetween($string){
//explode the string
$exploded=explode("'",$string);
//print using foreach loop or in any way you want
foreach($exploded as $explode){
echo $explode.'<br/>';
}
}