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?

Link to comment
Share on other sites

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
Share on other sites

(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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.