Problem with regular expressions and url's

Discussion in 'PHP' started by petershaman, Sep 24, 2008.

  1. #1
    I have a problem, want to make script which will cutting off from the page unwanted links, which are posted by users of my website.

    I now how to cut off name of this site, but have no idea how to get rid off "http://www." and everything after "/" slash in the address.

    Here's my code:
    $text = preg_replace("'(example.com)'i", '', $text);
    PHP:
    I'll be very grateful for help :)
     
    petershaman, Sep 24, 2008 IP
  2. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try the following.
    
    $text = preg_replace("http://example.com/(.*)?''i", '', $text);
    Code (markup):
     
    daman371, Oct 3, 2008 IP
  3. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope, that doesn't works at all :-(
     
    petershaman, Oct 3, 2008 IP
  4. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    $wrongurl="www.hotmail.com/asdasd/asdasd/adasd";
    $array=array("www.","http://");
    $text2=str_ireplace($array,"",$wrongurl);
    $str = explode('/', $text2);
    $text= $str[0];
    echo $text;
    ?>
    
    PHP:
    Try this, it will replace "http://", "www." and all letters after "/" in the url.
    For example in above code:
    1. "http://www.hotmail.com/asdasd/asdasd/adasd" will only show you "hotmail.com"
    2. "http://hotmail.com/asdasd/asdasd/adasd" will also show you only "hotmail.com"
    3. "www.hotmail.com/asdasd/asdasd/adasd" will also show you only "hotmail.com"

    Thanks, hope it helps
     
    mehdi, Oct 3, 2008 IP
  5. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's doesn't work neither, and bear in mind that I want to delete whole url
    http://www.example.com/something-after-slash
    HTML:
    and replace with message [link prohibited]
     
    petershaman, Oct 11, 2008 IP
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    So you dont want the whole link itself ? Basically if its a link its not displayed ?
     
    Bohra, Oct 11, 2008 IP
  7. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try this:
    <?php
    $wrongurl="www.hotmail.com/";
    $array=array("www.","http://");
    $text2=str_ireplace($array,"",$wrongurl);
    $str = explode('/', $text2);
    if ($str[1]){
    echo "[Link Prohibited!]";
    }else{
    echo "http://www.".$str[0];
    }
    ?>
    PHP:
    if entered url have any text after slash, it will replace it with "Link Prohibited".
     
    mehdi, Oct 11, 2008 IP
  8. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This doesn't work I'm getting
    http://www.hotmail.com
    HTML:
    at the top of every page. I give You broader look on the code which I have now:

    			     			      $text = ' ' . $text . ' ';
    			      $text = preg_replace("'example.com'i", '', $text);
    			      return $text; 
    HTML:
    This code works, I mean it cutting off links
    example.com
    HTML:
    BUT leaves
    http://www.
    HTML:
    and
    /something-after-slash
    HTML:
     
    petershaman, Oct 11, 2008 IP
  9. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Use this:
    
    $text = ' ' . $text . ' ';
                      $text="example.com";
    $array=array("www.","http://");
    $text2=str_ireplace($array,"",$text);
    $str = explode('/', $text2);
    if ($str[1]){
    $text= "[Link Prohibited!]";
    }else{
    $text= "http://www.".$str[0];
    }
                      return $text;
    
    PHP:
     
    mehdi, Oct 11, 2008 IP
  10. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Still the same, have no idea how to make it working :/
     
    petershaman, Oct 11, 2008 IP
  11. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Use regular expression

    $string = "Bla bla bla..Visit http://regex.kzzen.com for more info. <br />
    Visit http://kzzen.com/2008/10/if-you-want-to-earn-money-online/ for more info.<br />
    Visit http://www.kzzen.com/?act=what for more info.<br />
    Visit www.kzzen.com/?act=what for more info.<br />
    ";
    $pattern = "/http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    $string = preg_replace($pattern,'[Link Prohibited!]',$string);
    print $string;
    PHP:
    This output :
    Note that all links start with http:// were replaced. Without http:// it won't be replaced.
    And it stop replacing when it found a whitespace.

    You can use my Regex Tester and find a better solution, maybe.
     
    ads2help, Oct 11, 2008 IP
  12. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ads2help Your last solution works just like You said "all links with http:// are replaced with [Link prohibited] message", so it's all right then, but in my case I want to replace only links to a one particular website, is this possible?
     
    petershaman, Oct 11, 2008 IP
  13. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #13
    $rejected_website = 'http:\/\/www.example.com';
    $string = "Bla bla bla..Visit http://regex.kzzen.com for more info. <br />
    Visit http://www.example.com/something-after-slash for more info.<br />
    Visit http://www.example.com for more info.<br />
    Visit www.kzzen.com/?act=what for more info.<br />
    ";
    $pattern = "/$rejected_website(\/\S*)?/";
    $string = preg_replace($pattern,'[Link Prohibited!]',$string);
    print $string;
    PHP:
    Output :
     
    ads2help, Oct 11, 2008 IP
  14. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    All right its almost working, link
    http://www.example.com/something
    HTML:
    has been cutt off, and replaced with [link prohibitted] msg, but there are still links:

    http://example.com/something
    HTML:
    
    www.example.com/something
    HTML:
    and
    example.com/something
    HTML:
    --
    edit:
    I made up something like that:
    
    $rejected_website = 'example.com';
    $pattern = "/(http:\/\/www)?$rejected_website(\/\S*)?/";
    PHP:
    but I'm getting
    www.[Link prohibited]
    HTML:
    http://[Link progibited]
    HTML:
    or

    http://www.[Link prohibited]
    HTML:
     
    petershaman, Oct 15, 2008 IP
  15. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #15
    
    $rejected_website = 'example.com';
    $string = "Bla bla bla..Visit http://regex.kzzen.com for more info. <br />
    Visit http://www.example.com/something-after-slash for more info.<br />
    Visit http://www.example.com for more info.<br />
    Visit example.com for more info.<br />
    Visit http://example.com for more info.<br />
    Visit notrejected.com for more info.<br />
    Visit www.notrejected.com for more info.<br />
    ";
    $pattern = "/(http:\/\/)?(www.)*$rejected_website(\/\S*)?/";
    $string = preg_replace($pattern,'[Link Prohibited!]',$string);
    print $string;
    
    PHP:
    output :

    is this ok?
     
    ads2help, Oct 15, 2008 IP
  16. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #16
    close this thread its already been answered,,,
     
    bartolay13, Oct 15, 2008 IP
  17. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Hi ads2help That's works! I'm so much grateful to You! I warn You, that if I'd have some problems with reg. expressions, I'll call You :-D

    Cheers
     
    petershaman, Oct 16, 2008 IP