There is a log file in which there are a bunch of lines. Each has a substring of type REFER_ID=575616 .

I want to pull out all the referrals id using code

 ereg("(REFER_ID=\d*)", $str, $regs); 

In $regs only " REFER_ID= " is entered without numbers.

If in the regular expression to replace with " REFER_ID=\d* ", then $regs not formed at all (checked with the code isset ($regs)? print 1:print 0; )

Where am I wrong?

    1 answer 1

    can be so:

     $a='REFER_ID=575611 REFER_ID=575612 REFER_ID=575613 REFER_ID=575614 '; preg_match_all ('/REFER_ID=(\d+)/',$a,$m); print_r($m); 

    then in $ m [1] there will be an array of all IDs like this:

     Array ( [0] => Array ( [0] => REFER_ID=575611 [1] => REFER_ID=575612 [2] => REFER_ID=575613 [3] => REFER_ID=575614 ) [1] => Array ( [0] => 575611 [1] => 575612 [2] => 575613 [3] => 575614 ) ) 
    • I tried preg_match_all ("/ REFER_ID = (\ d +) /", $ str, $ matches); It turned out an empty array of type Array ([0] => Array () [1] => Array ()) Ps And how to insert the program code into the message? - zenith
    • you missed the slash: not (d+ , but (\d+ - jkeks
    • just paste it and then select it and click the button with zeros and ones - jkeks
    • In the end, it turns out quite a workable code, but for some reason the regular expression doesn’t hang out :( Of course you bent about the slash: it hid the site's engine. - zenith
    • In general, I found the error in the winter: preg_match_all was in a loop and $ matches was overwritten with empty search results. Changed to $ matches [] earned as it should. - zenith pm