Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regex/php to read text inside quotations

Tags:

regex

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?

like image 547
mrpatg Avatar asked Sep 02 '09 02:09

mrpatg


3 Answers

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"
  )

*/
?>
like image 65
opello Avatar answered Sep 30 '22 04:09

opello


<?php

$string = 'This User "The Title Of The Post"';

preg_match_all('/"([^"]+)"/', $string, $matches);

var_dump($matches);
like image 33
Jake McGraw Avatar answered Sep 30 '22 04:09

Jake McGraw


$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).

like image 40
Paige Ruten Avatar answered Sep 30 '22 06:09

Paige Ruten