0

i'm creating a photo sharing system with a particular photo temp verify. Basically, on input file submit photos are loaded into a dir called "temp".

I change the name of file in a particular string like this 3_1383310890_1709373616.jpg where 3 is the user_id 1383310890 is the date and 1709373616 is a random code name.

Now, if user doesn't complete the form... the next time, it should checks if exist a file temp for that user id and i would get info like this way but I do not understand why it does not work.

$string = 'temp/3_1383310890_1709373616.jpg';

    preg_match_all('/^[0-9]+_/',$string,$match);

    $arr = array('user' => $match[1][0], 'date' => $match[2][0], 'photo' => $match[3][0]);
    echo json_encode($arr);

it returns null objects

{"user":null,"date":null,"photo":null}

Any ideas ?

Mario Rossi
  • 59
  • 1
  • 11
  • 1
    Have you tried `var_dump($match)`? What's in there? – andrewsi Nov 01 '13 at 13:43
  • i think that the real problem is into preg_match_all, what are you think about it? – Mario Rossi Nov 01 '13 at 13:47
  • 1
    What are you trying to match with this regex? You are currently searching for "start of the string, followed by one or more digits, followed by an underscore". The start of the string is "temp", which doesn't match "one or more digits". – gen_Eric Nov 01 '13 at 13:48
  • That's what I'm trying to get you to find out - does your regex match what you think it's matching? If not, what does it match? – andrewsi Nov 01 '13 at 13:49
  • Damn, I'm so tired that I did not realize that i passed not correct string :D thanks anyway guys – Mario Rossi Nov 01 '13 at 14:06

1 Answers1

0

The ^ char. in this case means matching from the start of the string. I would use:

$string = 'temp/3_1383310890_1709373616.jpg';
preg_match('/^temp\/([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/',$string,$match);
$arr = array('user' => $match[0], 'date' => $match[1], 'photo' => $match[2]);
echo json_encode($arr);
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48