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?
<?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
site_ids==
from site_ids==34||sales_region==45
" (i.e. regex should return 34). I tried with preg_match('/^[^site_ids==]*||([^||]*)"$/', $str, $matches);
after $str = 'site_ids==34||sales_region==45';
and not getting result. I am not sure if |
needs escaping. The delimiter in the question is a double quote ("
) whereas in my case they are site_ids==
and ||
. What would be correct regex for me? Thanks.
Jun 20, 2011 at 13:43