bbmak Posted July 19, 2012 Share Posted July 19, 2012 I use simplexml_load_file() to grab the xml to a string and use preg_match_all to search all urls in the xml description. However, I want to add another condition in the preg_match_all, only match the URLs with the word 'thumb' in it. if (preg_match_all('!(href|src)="([^"]+)"!', $item_description, $matches)) { foreach ($matches[2] as $location) { ... } How do I change the preg_match_all expression? Link to comment https://forums.phpfreaks.com/topic/265970-search-all-urls-in-a-string-with-specific-word/ Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 Just use lazy 0,infinite quintifiers (href|src)="[^"]*?thumb[^"]*?" Link to comment https://forums.phpfreaks.com/topic/265970-search-all-urls-in-a-string-with-specific-word/#findComment-1362921 Share on other sites More sharing options...
bbmak Posted July 20, 2012 Author Share Posted July 20, 2012 Thank, It works. Just wondering are there any websites that give a detail explanation of those expressions. It actually drives me crazy. Link to comment https://forums.phpfreaks.com/topic/265970-search-all-urls-in-a-string-with-specific-word/#findComment-1362924 Share on other sites More sharing options...
bbmak Posted July 20, 2012 Author Share Posted July 20, 2012 Oh wait, my array got src in front of the URL, Can you strip this as well? Array ( [0] => Array ( [0] => src="http://...." ) [1] => Array ( [0] => src ) ) Link to comment https://forums.phpfreaks.com/topic/265970-search-all-urls-in-a-string-with-specific-word/#findComment-1362926 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 (href|src)="([^"]*?thumb[^"]*?)" I use RegexBuddy. Well worth the money IMO. (href|src)="([^"]*?thumb[^"]*?)" Options: dot matches newline Match the regular expression below and capture its match into backreference number 1 ?(href|src)? Match either the regular expression below (attempting the next alternative only if this one fails) ?href? Match the characters ?href? literally ?href? Or match regular expression number 2 below (the entire group fails if this one fails to match) ?src? Match the characters ?src? literally ?src? Match the characters ?="? literally ?="? Match the regular expression below and capture its match into backreference number 2 ?([^"]*?thumb[^"]*?)? Match any character that is NOT a ?"? ?[^"]*?? Between zero and unlimited times, as few times as possible, expanding as needed (lazy) ?*?? Match the characters ?thumb? literally ?thumb? Match any character that is NOT a ?"? ?[^"]*?? Between zero and unlimited times, as few times as possible, expanding as needed (lazy) ?*?? Match the character ?"? literally ?"? Link to comment https://forums.phpfreaks.com/topic/265970-search-all-urls-in-a-string-with-specific-word/#findComment-1362928 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.