marcbraulio Posted February 22, 2012 Share Posted February 22, 2012 Hey everyone! New member here still getting the hang of PHP. Any help is much appreciated. Suppose the array contains: array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); How do I get php to find the array item that contains let's say "css" and instead of returning just "css" it returns the entire string that has the "css" in it, instance "http://www.php.net/styles/site.css". I was thinking of using the following: preg_match('/[sOME_PCRE_PATTERN].css/', 'http://static.php.net/www.php.net/styles/site.css', $matches); Problem is since the links could be different and have all sorts of different characters in it, I have no idea what to put for the PCRE_PATTERN. In other words, this: $links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); foreach ($links as $key) { preg_match('/css/', $key, $matches); $results[] = $matches; } print_r ($results); Returns this: Array ( [0] => Array ( [0] => css ) [1] => Array ( ) [2] => Array ( ) ) I would like it to return: Array ( [0] => Array ( [0] => http://www.php.net/styles/site.css ) [1] => Array ( ) [2] => Array ( ) ) I hope that makes sense to you guys. Once again, any help is appreciated and thank you! Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/ Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 For something simple like that there's no need to use regular expressions. Loop through the array, using strpos to check if the value exists. If it does, 'push' the value into a $results array. Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/#findComment-1319865 Share on other sites More sharing options...
marcus Posted February 22, 2012 Share Posted February 22, 2012 $links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); $results = array(); foreach($links AS $link){ if(strpos($link, '.css') !== false){ $results[] = $link; } } Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/#findComment-1319868 Share on other sites More sharing options...
marcbraulio Posted February 22, 2012 Author Share Posted February 22, 2012 For something simple like that there's no need to use regular expressions. Loop through the array, using strpos to check if the value exists. If it does, 'push' the value into a $results array. Genius! Thank you! If anyone else is curious, this is the final working solution: Original array: $links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); Solution suggested by "xyph" foreach ($links as $key) { if (strpos($key,'css')) { $results[] = $key; } } Final Result: Array ( [0] => http://www.php.net/styles/site.css ) EDIT: Or use the solution above by "mgallforever" which I somehow didn't see until now. Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/#findComment-1319869 Share on other sites More sharing options...
marcbraulio Posted February 22, 2012 Author Share Posted February 22, 2012 $links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php'); $results = array(); foreach($links AS $link){ if(strpos($link, '.css') !== false){ $results[] = $link; } } Thank you very much =] Out of curiosity, is it considered a good practice to declare the $results = array(); before hand? Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/#findComment-1319870 Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 Yes, it's better practise. If $results is undefined, and you use $results[]... no error will be thrown. If $results is a defined non-array, and you use $results[]... an error will be thrown, possibly fatal. You need to use if( strpos(args) !== FALSE ). If 'css' are the first characters in your string, the 'offset' strpos returns would be 0, which translates to FALSE unless you use a STRICT (!==) comparison. It's explained in more detail in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/#findComment-1319872 Share on other sites More sharing options...
marcbraulio Posted February 22, 2012 Author Share Posted February 22, 2012 Yes, it's better practise. If $results is undefined, and you use $results[]... no error will be thrown. If $results is a defined non-array, and you use $results[]... an error will be thrown, possibly fatal. You need to use if( strpos(args) !== FALSE ). If 'css' are the first characters in your string, the 'offset' strpos returns would be 0, which translates to FALSE unless you use a STRICT (!==) comparison. It's explained in more detail in the manual. Got it! =] Quote Link to comment https://forums.phpfreaks.com/topic/257512-help-with-phps-pcre-patterns/#findComment-1320220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.