php help

Discussion in 'Web Design & Coding' started by jimi_81, Jan 10, 2005.

  1. jimi_81

    jimi_81 Moderator Political User

    Messages:
    820
    Location:
    Stoney Creek, ON, Canada
    simple question perhaps..
    i was wondering how this input window creates hyperlinks just by the text typed... i imagine its a regular expression that parses the text..

    is there an example snippet of this being done.
    thanks
     
  2. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    This helped me out a lot when I created a news submission system back in the past. Due to a lost backup (I know how you feel EP! :p) I can't give you an example.
     
  3. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    ignoring bbtags, I would just whack anchor tags around strings begining with "www." or "http". But I'm simple what way
     
  4. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    PHP:
    <?php
    $text 
    ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                         
    "<a href=\"\\0\">\\0</a>"$text);
    ?> 
    like that? me too :D
     
  5. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    jimi_81 likes this.
  6. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    let me fix my server to serve up PHP source files again.

    Done.
     
  7. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I am sure its stuff like that that just scares the bejeebus out of people :p
     
  8. jimi_81

    jimi_81 Moderator Political User

    Messages:
    820
    Location:
    Stoney Creek, ON, Canada
    digging up an old thread here...(just over a month :) )

    ok, just to be sure...

    if i have a textarea, called $news.

    say...
    $news = "blah blah [bold]hey[/bold], and www.blah.com is blah blah blah";

    do the reg ex's listed above parse $news and find each 'pattern'...the bold tag, and the link tags, etc.

    and also, to make sure this would be done after selecting the data from the database.

    any clarification, thanks
     
  9. jimi_81

    jimi_81 Moderator Political User

    Messages:
    820
    Location:
    Stoney Creek, ON, Canada
    ok kill thread, got it figured out. thanks for the help guys!
     
  10. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Sure as hell did when i first looked at regular expressions.

    It is actually quite simple.

    Code:
    !(^|\s)((.+)://(\w+[^\[\]\s/]+\.\w{2,}\.?(:\w+[^\[\]\s/])?)/?(\w+[^\[\]\s]+)?)!i
    Dissected below.

    Code:
    !(^|\s)
    This tells it to match only if there is a space before it, or an \n. Otherwise, we don't do matching, this way text like "blahhttp://x-istence.com/" does not get matched.

    The ! is the starting of the expression. It could also be a # symbol, it does not make a difference, as long as it is not one of the reserved ones: []\|(). Basically, use common sense. If you choose one you will rarely use in your pattern, then you won't have to do all the backward slashes to undo what you did. I use ! in this example.

    Code:
    (
    A simple brace, it means everything inside this has to be available in the backwards slashes for us to use, for instance, the space above is available in \\1, so this will be \\2.

    Code:
    (.+)://
    Match the <anythinghere>:// part. Straightforward, .+ means one or more characters.

    This one is available (protocol only, not the ://) at \\3

    Code:
    (
    Opening brace, this one will be available at \\4

    Code:
    \w+[^\[\]\s/]+\.\w{2,}\.?
    This code is fairly simple. \w+ means to match one of any character at all, but we want to limit that, as we don't want spaces. So we add a bracket, and as the first symbol put a caret (^). This means that the rest should cause it to NOT match. So we don't want to match if there is a [, ], \s (any type of space). We add a + to the end of the stuff in brackets, this means it can match any number of characters (Or in this case, the opposite, not match those).

    Next is the \. This is just a way to show that we want to match the literal period.

    So far we have matched: www.somedomain

    We want to match a . for com, net, org or any other, the \w{2,} means we want to match 2 or more characters at least, so that means we can also match hello.nl.

    The last \.? is to match if there is an extra period in the domain, which is allowed to mean the literal domain, most browsers support this, so that URL's like this will work: http://osnn.net./index.php

    Code:
    (:\w+[^\[\]\s/])?
    Look at the above for an explenation, all we are doing here is looking for the optional port number, so that matches like http://osnn.net:80/ will work flawlessly. The ? at the end means it can match 0 or more times. In most cases it will not be matched, and \\5 will be empty, specifying that no port is available.

    Code:
    )
    Close \\4. This is used in our pattern to match just the hostname and port number, to make slashdot style links.

    Code:
    /?(\w+[^\[\]\s]+)?
    We look for the optional stuff following the domain name, so that things like http://example.net/test/blah.html is also used when making it into a URL.

    Code:
    )!i
    Close the entire thing, now \\2 is filled with goodiness, namely the entire URL without the extra space at the beginning which is stored in \\1.

    The ! means it is the end of the pattern, the extra "i" is to make the pattern case insensitive. For more pattern modifiers check out http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php.

    I hope this helps just a little bit :D.
     
  11. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    someone glue that post somewhere - its going to be by regexp bible for the next millienia :D
     
  12. Xie

    Xie - geek - Subscribed User Folding Team

    Messages:
    5,275
    Location:
    NY, USA
    My head hurts now it's so simple. :p
     
  13. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Happy to be off service, my dads company makes Aleve, make sure to buy some :p
     
  14. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    btw, I recommend "Beginning Perl" by James Lee for really understanding Perl regular expressions
     
  15. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Sorry to bump, however, how would I use this on a string ?

    For example I have
    PHP:
    $SomeVar "text text text blah http://k-thx.com/mooses/ text text blah http://something.com blah blah";
    Is there anyway to use the preg_replace you've written above to pick out and link the URI's in that string?
     
  16. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    PHP:
    $SomeVar "text text text blah http://k-thx.com/mooses/ text text blah http://something.com blah blah";
    $text preg_replace"`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i""<a href=\"http\\3://\\5\\6\\8\\9\" title=\"\\0\">\\5\\6</a>"$SomeVar);
    try that?
     
  17. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    That would indeed appear to work :)
     
  18. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Just a note. My pattern above did not work for things that contained multiple URL's in one line, or text for that matter, it would render it incorrectly. The correct version is as follows:

    Code:
    <?php
    
    $pattern = "!((([^\[\]\s/]+)+)://([a-zA-Z\-\:\@\.]+\.[a-zA-Z]{2,}\.?((:\d+)?))/?\.?([a-zA-Z0-9-\+\?\!\@#\$%\^&\*\./=_:]+)?)!i";
    $urlit = "<a href=\"\\1\" alt=\"\\4\" rel=\"nofollow\">\\1</a> [\\4]";
    
    $url[] = "http://x-istence.com/";
    $url[] = "http://x-istence.com/****it/something.php";
    $url[] = "http://personal.x-istence.com/";
    $url[] = "http://personal.x-istence.com/blog";
    $url[] = "http://personal.x-istence.com";
    $url[] = "irc://irc.osnn.net/osnn";
    $url[] = "ftp://xistence:bloody@hello.com/";
    $url[] = "javascript://alert(You are Hacked);.com/";
    $url[] = "mailto://xistence@x-istence.com";
    $url[] = "mailto:xistence@x-istence.com";
    $url[] = "http://somet/microsoft.com/";
    $url[] = "http://x-istence.com/some%20document%20withspaces.doc";
    $url[] = "https://x-istence.com/some document withspaces.doc";
    $url[] = "http://xistence.breached.x-istence.com:81/new.x-istence.com/";
    $url[] = "http://xistence.breached.x-istence.com:81/new.x-istence.com/news.php?cwhat=comments&cid=7";
    $url[] = "http://x-istence.com./index.php";
    $url[] = "http://x-istence.com/.index.php";
    $url[] = "https://blah.com/index";
    $url[] = "http://mysite.defiant.osnn.net/";
    $url[] = "Hello this is a bunch of text. With two URL's in it: http://x-istence.com/ and http://xistence.breached.x-istence.com";
    
    foreach($url as $urlval)
        echo "<br>" .  preg_replace($pattern, $urlit, $urlval);
    
    ?>
     
    Last edited: Aug 28, 2005
  19. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I use this for regex http://www.regular-expressions.info/

    btw, I have a BBCodeParser php class which I am going to tweak a little further, document and then release here if people are interested.