regex - Basic issue with PHP preg_match -
i have following array in php:
$item_array = array("facebook/1377445751.jpg_t","twitter/1377446022.gif_s","flickr/1377531219.png_w","flickr/1377531219.jpg_t_w"); i iterating through array, removing values don't match following regex:
(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w) here code:
foreach($item_array $key => $item) { if(!preg_match('/(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)/', $item)) { unset($item_array[$key]); } } however, when use var_dump() on $item_array, obtain empty array. since preg_match doesn't match of array values, i'm under impression problem lies there, though i've tested multiple times regexr.
any ideas on might going wrong?
as can see here (click), regex works fine. perhaps having issue have not stated deals using result.
i suspect might looking $matches parameter, store results.
use so:
preg_match('/(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)/', $item, $matches); var_dump($matches); this working described. cleaned little bit, code same. must have different problem.
$item_array = [ "facebook/1377445751.jpg_t", "twitter/1377446022.gif_s", "flickr/1377531219.png_w", "flickr/1377531219.jpg_t_w", "other stuff won't match!" ]; foreach($item_array $key => $item) { $foo = preg_match('/(facebook|twitter|flickr)\/(\d{10})\.(jpg|png|gif)_(t_w|t|s|w)/', $item); if(!$foo) { echo $item; //displays "other stuff won't match" unset($item_array[$key]); } } var_dump($item_array); //non match removed
Comments
Post a Comment