preg_match for a specific pattern in a url

Discussion in 'PHP' started by ironmankho, Apr 1, 2014.

  1. #1
    I would like to use preg_match in PHP to test the format of a URL. The URL looks like this:

    <a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a> 
    Code (markup):
    well honestly I have no idea of preg_match creating but my goal is

    a pattern start with <a href= contain word ~dead host~ end with </a>

    I try string contain in php native function but unfortunately it was not smart so I think preg_match is the only choice.
     
    Solved! View solution.
    ironmankho, Apr 1, 2014 IP
  2. #2
    Here is a basic PHP script I came up with that uses preg_match to see if the url matches the regular expression: "<a href=.*~dead host~.*</a>"
    which basically means start with "<a href" contain "~dead host~" somewhere in the middle, and end with "</a>". I hope that is what you are looking for. You just need to set the $url variable to the url are are trying to check.

    $url = "<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>";
    $pattern = "@<a href=.*~dead host~.*</a>@";
    
    if(preg_match($pattern, $url))
        print "Valid Format";
    else
        print "Invalid Format";
    Code (markup):
     
    tylerman169, Apr 1, 2014 IP