preg_match Find on Page

Discussion in 'PHP' started by Silver89, Nov 22, 2008.

  1. #1
    I'm looking to use preg_match to find:

    href="http://mostplays.com"

    On a page. However I'm not sure exactly how to do that with preg_match.


    Basically if it finds it on a page then show true, else show false.

    Probably very simple with and if else.
     
    Silver89, Nov 22, 2008 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    If you are searching for exact string, strpos is faster and more preferable than preg_match.

    Use
    if (strpos($text,'href="http://mostplays.com"') !== false)
     echo 'exists';
    else
     echo 'no';
    PHP:
     
    wmtips, Nov 22, 2008 IP
    Silver89 likes this.
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    preg_match will return 1 if a match is found or false/zero otherwise, so you can safely do something like this logic-wise with preg_match.

    if(preg_match(...))
    {
       // do stuff
    }
    Code (markup):
    If you're searching for an exact string, use strpos or stripos like wmtips suggested.

    However, if this is for something like a backlink verification where site owners may vary the way they enter their HTML, pattern matching is easier to maintain than a strpos loop.

    if(preg_match('#href\s*=\s*([\'"])http://domain\.com/$1#i', $str))
    {
    
    }
    Code (markup):
     
    joebert, Nov 22, 2008 IP
    Silver89 likes this.