1

Sorry for the title, I don't know how to explain it better.

I must get 354607 from the following string:

...jLHoiAAD1037354607Ij0Ij1Ij2...

The "354607" is dynamic, but it has the "1037" in any case before, and is in any case exactly 6 characters long.

The problem is, the string is about 50.000 up to 1.000.000 characters long. So I want a resource-friendly solution.

I tried it with:

preg_match_all("/1037(.*?){0,5}/", $new, $search1037);

and:

preg_match_all("/1037(.*?{0,5})/", $new, $search1037);

but, I don't know how to use regular expressions correctly.

I hope someone could help me!

Thank's a lot!

kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
C.E.
  • 664
  • 2
  • 5
  • 21

2 Answers2

3

Use, \d{6} represents 6 numbers

preg_match_all("/1037(\d{6})/", $new, $search1037);

returns an array with

array(
    0   =>  array(
        0   =>  1037354607
    ),
    1   =>  array(
        0   =>  354607
    )
)

Check this demo

davcs86
  • 3,926
  • 2
  • 18
  • 30
  • Thank you!! Saved me a lot of time and ugly workaround code! – C.E. Sep 01 '15 at 01:54
  • Hy davcs86, can i ask you another question? If i try to search for "06A906032HN" where the numbers (except 9060) and alphanumerics are variable, but everytime in the same format eg XX(X or Z)9060XXZZ (X stands for an INTENT and Z for alphanumeric (letter)) .. Did you know, how to deal with it ? It could be some like this in my string: 06A906032HN, 05B906032BC, 036906012AL – C.E. Sep 02 '15 at 22:36
  • use `/(\d{2}[a-zA-Z\d]9060\d{2}[a-zA-Z\d]{2})/`, where `\d` is for digits and `[a-zA-Z\d]` is for alphanumeric (lower and uppercase), http://www.phpliveregex.com/p/cux – davcs86 Sep 02 '15 at 22:49
  • wow, super fast answer! Thank you! I only forget, better recognized one thing yet: The last 2 alphanumerics can be 1 or 2 characters long. Can this be integrated ? – C.E. Sep 02 '15 at 22:52
  • lol, I was around here. In the last {2}, you can set a range like {1,2} to specify 1 to 2 characters, or {2,5} for 2 to 5 chars, so on. – davcs86 Sep 02 '15 at 22:55
  • I try around, but i dont get it to work :( And i found a few other exceptions. Would be very very fine from you if you can help me with this: The string could be for exception: 8P0907115H, 06A906032HN, 03L906018A, 03G906012B, and so on..It looks like 90 is the only constante, and the last 1 or 2 alphanumerics – C.E. Sep 02 '15 at 23:11
  • Okay, think i got it !! Thanks for your help, it learned me a little bit more about regex! I used this to solve: "\d{1}[a-zA-Z\d]{2}90\d{4}[a-zA-Z]{1,2}" Big thanks again! – C.E. Sep 02 '15 at 23:24
2

Since you're concerned with finding a resource-friendly solution, you may be better off not using preg_match. Regular expressions tend to require more overhead in general, as discussed in this SO question.

Instead, you could use strstr():

$string = strstr($string,'1037');

Which will return the first instance of '1037' in $string, along with everything following it. Then, use substr():

$string = substr($string,4,6);

Which returns the substring within $string starting at position 4 (where position 0 = 1, position 1 = 0, position 2 = 3, position 3 = 7, position 4 = beginning of 6 digits) and including 6 characters.

For fun, in one line:

$string = substr(strstr($string,'1037'),4,6);
Community
  • 1
  • 1
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27