Jump to content

Search all URLs in a string with specific word.


bbmak

Recommended Posts

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?

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
        )

)

(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 ?"?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.