Help with PHP if contains

Discussion in 'PHP' started by lowridertj, Apr 23, 2007.

Thread Status:
Not open for further replies.
  1. #1
    if i wanted to block url links or statements from a content post how do i do this

    Example.

    in the text bar someone places in http://www.sitename.com or www.sitename.com or sitename.com

    mainly this would look for
    http://
    www.
    .com

    if it contains this information then it will not allow the post. and or it changes the entire url

    http://www.sitename.com or www.sitename.com or sitename.com


    to censored or something..

    Can anyone help?


    Thanks in advance..
    TJ
     
    lowridertj, Apr 23, 2007 IP
  2. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    i found this function for you at php.net under preg_match
    function bigworldhostname($url){
        preg_match('@^(?:http://)?([^/]+)@i',$url, $matches);
       
        $host = $matches[1];
    
        preg_match('/[^.]+\.[^.]+$/', $host, $usHost);
        
        if (strlen($usHost[0])>5){
            return $usHost[0];
        }
        else {
            preg_match('/[^.]+\.[^.]+\.[^.]+$/', $host, $restofWorldHost);
            return $restofWorldHost[0];
        }
    }
    PHP:
     
    pepsipunk, Apr 24, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    How about this? I ignored URLs like this: sitename.com Because some people don't put spaces after their sentences, and it wouldn't let them post then if they typed something like looked like an URL.

    
    $text = preg_replace('/(((ht|f)tps?:\/\/)|(www\.))([\w-\.]+)\.[a-z]{2,4}[^\r\n\s\t"]*/ie', "str_repeat('*', strlen('$0'))", $text);
    
    
    PHP:
    If you want to check for only .com domains, you can also do this:
    
    if (preg_match('/(https?|ftp|www\.|\.com)/i', $text))
    {
    	// Throw error
    }
    
    PHP:
    I think this was your initial idea, but I like the replacement thing better.
     
    nico_swd, Apr 24, 2007 IP
  4. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #4
    thanks very much exactly what i was looking for.
     
    lowridertj, Apr 24, 2007 IP
  5. Wyla

    Wyla Well-Known Member

    Messages:
    924
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Wyla, Apr 24, 2007 IP
  6. reza56

    reza56 Active Member

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #6
    reza56, Apr 24, 2007 IP
Thread Status:
Not open for further replies.