Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display http:// in textarea as links on output

    Hi all.

    I have a guestbook im making. Im not much of a coder yet, its actually my first real attempt to make something useful in php & MySQL.

    I got a simple insert and display data from a MySQL script working fine, but the problem is, when i write a link in a textarea: hxxp://www.google.com, it displays as text when i output it from my MySQL table.
    I want the users of the guestbook to just write an url without having to use the html <a> tag. So when writing an url, it automatically displays as a link on the output.

    My form looks like this:
    Code:
    <form action="insertcomment.php" method="post" name="post">
    <textarea name="comment" cols="53" rows="5" value="" /></textarea>
    <br />
    <input type="Submit" value="Post comment" class="button" />
    </form>
    The insert comment to MySQL file looks like this:
    Code:
    <?php 
    
    $_POST[comment] = mysql_real_escape_string(nl2br($_POST["comment"]));
    
    $sql="INSERT INTO table_name(comment) VALUES ('$_POST[comment]')";
    
    if (!mysql_query($sql))
      {
      die('Error: ' . mysql_error());
      }
      echo "<meta http-equiv='REFRESH' content='0;url=main.php'>";
    echo "Success. <a href='main.php'>Go back</a>";
    
    mysql_close($con)
    
    ?>
    The database output file looks like this:
    Code:
    <?php
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error());
    
    $sql="SELECT * FROM table_name ORDER BY commentid DESC LIMIT 10";
    $result=mysql_query($sql);
    
    while($rows=mysql_fetch_array($result)){
    
    echo $comment;
    }
    ?>
    If its something you dont understand of what i just said, then aim and fire questions at me.
    Thanks in advance for any help

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,387
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    I found this on the internet ... can't remember the source though ...
    Something I had saved on my PC.

    Copy and paste, upload it as "test.php" and see how it works:
    PHP Code:
    <?php

    // Here is the test string with three types of links ...

    $string "
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
    nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam
    erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci
    tation ullamcorper suscipit www.google.com ut aliquip ex ea commodo
    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
    molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero
    eros et accumsan et iusto odio dignissim qui blandit praesent luptatum
    zzril delenit augue duis dolore te feugait nulla facilisi.
    <br /><br />
    Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet
    doming id quod mazim placerat facer possim assum. Email me: john@aol.com
    <br /><br />
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
    nonummy nibh euismod http://www.yahoo.com ut laoreet dolore magna aliquam erat
    volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullam
    corper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
    <br /><br />
    "
    ;

    //echo makeclickablelinks($string);
    echo clickable_link($string);

    function 
    makeclickablelinks($text) {
    $text eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="\\1">\\1</a>'$text);
    $text eregi_replace('((www.)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="http://\\1">\\1</a>'$text);
    $text eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>'$text);
    return 
    $text;
    }

    function 
    clickable_link($text)
    {
    # this functions deserves credit to the fine folks at phpbb.com

    $text preg_replace('#(script|about|applet|activex|chrome):#is'"\\1:"$text);

    // pad it with a space so we can match things at the start of the 1st line.
    $ret ' ' $text;

    // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
    // xxxx can only be alpha characters.
    // yyyy is anything up to the first space, newline, comma, double quote or <
    $ret preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is""\\1<a href=\"\\2\" target=\"_blank\">\\2</a>"$ret);

    // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
    // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
    // zzzz is optional.. will contain everything up to the first space, newline,
    // comma, double quote or <.
    $ret preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is""\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>"$ret);

    // matches an email@domain type address at the start of a line, or after a space.
    // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
    $ret preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i""\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>"$ret);

    // Remove our padding..
    $ret substr($ret1);
    $ret str_replace(">http://www.",">",$ret);
    $ret str_replace(">www.",">",$ret);
    return 
    $ret;
    }

    ?>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •