我的文字标题是
This User "The Title Of The Post"
我想抓住INSIDE中的引号,并将其存储在变量中。我如何用正则表达式和php做到这一点?
答案 0 :(得分:9)
<?php
$x = 'This User "The Title Of The Post"';
preg_match('/".*?"/', $x, $matches);
print_r($matches);
/*
Output:
Array
(
[0] => "The Title Of The Post"
)
*/
?>
答案 1 :(得分:1)
<?php
$string = 'This User "The Title Of The Post"';
preg_match_all('/"([^"]+)"/', $string, $matches);
var_dump($matches);
答案 2 :(得分:1)
$string = 'This user "The Title Of The Post"';
$its_a_match = preg_match('/"(.+?)"/', $string, $matches);
$whats_inside_the_quotes = $matches[1];
$its_a_match
如果成功匹配则为1
,否则为0
。 $whats_inside_the_quotes
将包含正则表达式中括号中匹配的字符串。
如果它有点不清楚(它是),preg_match()实际上给$matches
(第三个参数)赋值。
答案 3 :(得分:1)
$str = 'This User "The Title Of The Post"';
$matches = array();
preg_match('/^[^"]*"([^"]*)"$/', $str, $matches);
$title = $matches[1];
echo $title; // prints The Title Of The Post