Asked  7 Months ago    Answers:  5   Viewed   49 times

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.



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.


$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
Wednesday, March 31, 2021
answered 7 Months ago

For this kind of job, I'll use preg_match_all:

$str = 'aaaaa: lorem ipsum bb: dolor sit amet ccc: no pro movet';
preg_match_all('/S+:.+?(?=S+:|$)/', $str, $m);


    [0] => Array
            [0] => aaaaa: lorem ipsum 
            [1] => bb: dolor sit amet 
            [2] => ccc: no pro movet



S+:        : 1 or more NON space followed by colon
.+?         : 1 or more any character not greedy
(?=S+:|$)  : lookahead, make sure we have 1 or more NON space followed by colon or end of string
Saturday, May 29, 2021
answered 5 Months ago

This is exactly how I would solve this problem. It's only worth worrying about the code being slow if you have a problem with the application being slow, and you can trace it down to the trim statements.

A well known programming quote is "Premature Optimisation is the root of all evil" - see the wikipedia article linked for more on this.

Saturday, May 29, 2021
answered 5 Months ago

This regex will do the trick:

(d+)d (d+)h (d+)m (d+)s

Each value (day, hour, minute, second) will be captured in a group.

About your regex: I don't know what do you mean by "isn't correct", but I guess it's probably failing because your regex is greedy instead of lazy (more info). Try using lazy operators, or using more specific matches (d instead of ., for example).


I need them to be separate variables

After matching, they will be put in different locations in the resulting array. Just assign them to variables. Check out an example here.

If you have trouble understanding the resulting array structure, you may want to use the PREG_SET_ORDER flag when calling preg_match_all (more information here).

Saturday, May 29, 2021
answered 5 Months ago

Regular Expressions. Or by splitting the string in a more tedious fashion.

Since I'm not a big regex-junkie, I would likely get the text-equivalent using .text(), then split the result on ":", and grab the second index (which would be the 'Spartan' text).

Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :