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_match
<?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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With