I have a text title that reads
This User "The Title Of The Post"
I want to grab just whats INSIDE of the quotation marks, and store it in a variable. How would i do this with regex and php?
I have a text title that reads
This User "The Title Of The Post"
I want to grab just whats INSIDE of the quotation marks, and store it in a variable. How would i do this with regex and php?
http://www.php.net/preg%5Fmatch
<?php
$x = 'This User "The Title Of The Post"';
preg_match('/".*?"/', $x, $matches);
print_r($matches);
/*
Output:
Array
(
[0] => "The Title Of The Post"
)
*/
?>
<?php
$string = 'This User "The Title Of The Post"';
preg_match_all('/"([^"]+)"/', $string, $matches);
var_dump($matches);
$string = 'This user "The Title Of The Post"';
$its_a_match = preg_match('/"(.+?)"/', $string, $matches);
$whats_inside_the_quotes = $matches[1];
$its_a_match
will be 1
if it made a successful match, otherwise 0
. $whats_inside_the_quotes
will contain the string matched in the set of parentheses in the regex.
In case it's a bit unclear (it is), preg_match() actually gives a value to $matches
(the third argument).
$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