0

I have a complex string like this

{},\"employees\":{},\"idIwant\":{\"2545\":{\"attributes\":{\"offset\":9855,

I need the sweet 2545 out from this string, I tried using regex, and strpos but it doesn't play nice with lots of colons, bracket, slashes. Is it possible to extract the number after idIwanti.e 2545 ?

This is actually coming from a website's source code, and this is not a json but a redux state string.

Dhiraj
  • 2,687
  • 2
  • 10
  • 34
  • This is actually coming from a source code, and this is not a json but a redux state string. – Dhiraj Jun 08 '19 at 04:35
  • Can you load/parse the JSON string? Please post a complete excerpt. Is it always `idIwant`, or will there be other keys/value types? Thanks for the clarification. – ggorlen Jun 08 '19 at 04:37
  • 1
    Should you need it, Here is a cheat sheet of some regex examples https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 – Raj Jun 08 '19 at 04:47

4 Answers4

2

This will work if idIwant has only numbers.

$string = '{},\"employees\":{},\"idIwant\":{\"2545\":{\"attributes\":{\"offset\":9855,';

preg_match('/idIwant.*?(\d+)/', $string, $matches);

echo $matches[1];

Test

azeós
  • 4,853
  • 4
  • 21
  • 40
2

Isolate the digits after the match like this:

Code: (Demo)

$string = '{},\"employees\":{},\"idIwant\":{\"2545\":{\"attributes\":{\"offset\":9855,';

echo preg_match('~"idIwant\\\":{\\\"\K\d+~', $string, $out) ? $out[0] : 'bonk';

Output:

2545

Keeping the " and \" around your sought key is important so that you are matching the whole targeted keyword (no inadvertent substring matching).

\K restarts the fullstring match so that you don't need to bloat the output array with unnecessary elements.

Php requires 3 or 4 \ to represent one in the pattern. (Here's some breakdown: https://stackoverflow.com/a/15369828/2943403)

p.s. Alternatively, you can force the leading portion of the pattern to be literally interpretted with \Q..\E like this:

Demo

echo preg_match('~\Q\"idIwant\":{\"\E\K\d+~', $string, $out) ? $out[0] : 'bonk';

Or if you are scared off by so many metacharacters, you could reduce the stability of your pattern and just match the search string, then match one or more non-digits, then forget the previously matched characters, then match 1 or more digits:

echo preg_match('~idIwant\D+\K\d+~', $string, $out) ? $out[0] : 'bonk';
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
1

most easy way is:

$String ='{},\"employees\":{},\"idIwant\":{\"2545\":{\"attributes\":{\"offset\":9855,';

$arr1 = explode('"idIwant\":{\"', $String);

if you ouput $arr1[1] you would get:

string=> 2545\":{\"attributes\":{\"offset\":9855,';

you need:

$arr2 = explode('\":{\"', $arr1[1]);

and you would get on $arr2[0]:

string=> 2545

if the string have a strict syntax

0

There are so many ways to get that desired digits, one would be an expression similar to:

.+idIwant\\":{\\"(.+?)\\.+

Demo

Test

$re = '/.+idIwant\\\\":{\\\\"(.+?)\\\\.+/m';
$str = '{},\\"employees\\":{},\\"idIwant\\":{\\"2545\\":{\\"attributes\\":{\\"offset\\":9855,';
$subst = '$1';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;
Emma
  • 27,428
  • 11
  • 44
  • 69