How to extract a string from double quotes?
Asked Answered
M

6

34

I have a string:

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

How can I extract the string in between the double quotes and have only the text (without the double quotes):

Your Balance left $0.10

I have tried preg_match_all() but with no luck.

Microbalance answered 19/6, 2009 at 9:17 Comment(1)
You might find s($str)->between('"', '"') helpful, as found in this standalone library.Kosygin
P
80

As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern

  • Double-quote
  • At least one non-double-quote
  • Double-quote

The brackets around the [^"]+ means that that portion will be returned as a separate group.

<?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
Ptolemy answered 19/6, 2009 at 9:28 Comment(1)
If you want to use it as an echo you can use preg_replace instead, ie: {{ preg_replace('/"([^"]+)"/', '$1', $str) }} (Laravel) or echo preg_replace('/"([^"]+)"/', '$1', $str);Pentaprism
L
20

For everyone hunting for a full featured string parser, try this:

(?:(?:"(?:\\"|[^"])+")|(?:'(?:\\'|[^'])+'));

Use in preg_match:

$haystack = "something else before 'Lars\' Teststring in quotes' something else after";
preg_match("/(?:(?:\"(?:\\\\\"|[^\"])+\")|(?:'(?:\\\'|[^'])+'))/is",$haystack,$match);

Returns:

Array
(
    [0] => 'Lars\' Teststring in quotes'
)

This works with single and double quoted string fragments.

Lightner answered 19/8, 2011 at 14:50 Comment(2)
Works but is there a way to exclude the quotes itself from the returned string?Chiron
Works nice except empty strings are not counted and get results messy. for example code let a="";let b=""; the first match will not be an empty result but it would rather be ";let b="Dash
P
10

Try this :

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

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

Portis answered 19/6, 2009 at 9:28 Comment(0)
E
7

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

$content = stripslashes(preg_match('/"((?:[^"]|\\\\.)*)"/'));
Eberta answered 19/6, 2009 at 10:52 Comment(0)
S
-1

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];
Sarver answered 19/6, 2009 at 9:26 Comment(1)
Double quotes are being used as pattern delimiters in this snippet. Do not use this snippet in your project.Acapulco
Y
-9

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.

Yarndyed answered 19/6, 2009 at 9:19 Comment(1)
Its not event a valid regex!Lethargic

© 2022 - 2024 — McMap. All rights reserved.