views:

1162

answers:

5

I have a string

This is a text, "Your Balance left $0.10", End 0

how can i extract the string in between double quote and have only the text 'Your Balance left $0.10' (without the double quotes)

Tried preg_match_all but no luck

A: 

Just use str_replace and escape the quote:

str_replace("\"","",$yourString);

Edit:

Sorry, didnt see that there was text after the 2nd quote. In that case, I'd simply to 2 searches, one for the first quote and one for the 2nd quote, and then do a substr to extra all stuff between the two.

Visage
A: 

The regular expression '"([^\\"]+)"' will match anything between two double quotes.

$string = '"Your Balance left $0.10", End 0';
preg_match('"([^\\"]+)"', $string, $result);
echo $result[0];
Rich Adams
+1  A: 

You can do this easily using a regular expression. "([^"]+)" will match the pattern

  • Double-quote
  • Any number of not double-quotes
  • Double-quote

The brackets around the [^"]+ means that that portion will be returned as a separate match, which is what we want.

<?php

$str  = 'This is a text, "Your Balance left $0.10", End 0';

//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
    print $m[1];   
} else {
   //preg_match returns the number of matches found, 
   //so if here didn't match pattern
}

//output: Your Balance left $0.10
Tom Haigh
+3  A: 

Try this :

preg_match_all('`"([^"]*)"`', $string, $results);

You should get all your extracted strings in $results[1].

A: 

Unlike other answers, this supports escapes, e.g. "string with \" quote in it".

$content = stripslashes(preg_match('/"((?:[^"]|\\\\.)*)"/'));
porneL