SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex needed urgently

    Hi

    I need a regex to match some urls with certain word in it. For example, if there is a url like http://www.wordpress.com and I need to find all such links on a particular site with the word wordpress in it. The regex I have gives me only the word wordpress but I want the value of the entire url.

    The code is:

    if (preg_match("/wordpress/", "#word", $matches)) {
    echo "Match was found <br />";
    echo $matches[0];

    }

    $word is where the urls are fetched from a for loop applied.

    Now matches[0] give me only the word wordpress whereas I need http://www.wordpress.com.

    Also if same url is there twice it should not be repeated i.e. if I have the link http://www.wordpress.com at two places on my site, it should give me that only once.

    thanks

  2. #2
    Non-Member
    Join Date
    Aug 2009
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will have to write a regexp that matches all possible ways to write the URL in questions, for instance [a-z:/]*.wordpress.com or something like that

  3. #3
    SitePoint Zealot Ripe's Avatar
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.txt2re.com/index-php.php3

    This site is extremely useful for writing regex

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code:
    For Each Link In Page
    [
        If Link Not In Array
        [
            Add Link To Array
        ]
    ]
    For Each Link In Array
    [
        If Link Contains Word
        [
            #do Stuff
        ]
    ]
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •