Jump to content

Recommended Posts

Here is what I have so far:

 

$query = "SELECT * FROM band ORDER BY Name ASC";

$result = mysql_query($query);

while($band = mysql_fetch_array($result)) {





       echo "<p>" . "<b>" . $band['Name'] . "</b>";


       echo "<br>" . $band['website'] . "</br>";


       echo "<br>" . $band['Description'] . "</br>";

 

 

I want to make it so when displayed, it finds all instances of a link, and auto converts to a hyperlink (when entered for the website www.example.com it comes back http://www.example.com etc.)

 

I have found dozens of examples online but I don't know how to retype them to make it work for mine.

 

Many thanks!!!

Assuming $band['website'] is the URL to the website you don't need to "find"  and convert them. Just do this:

 

 

<?php
 
$query = "SELECT * FROM band ORDER BY Name ASC";
$result = mysql_query($query);
while($band = mysql_fetch_array($result))
{
    echo "<p><b>{$band['Name']}</b><br>\n";
    echo "<a href=\"{$band['website']}\">{$band['website']}</a></br><br>\n";
    echo "{$band['Description']}</br>\n";
}

Thank you thats great! However, I would like it in case someone just leaves off the "http://" and stuff, then it will auto add that to it. Also I wanted to use it for the "Description" field too because thats a textarea and some links may be typed in it.

This is what I got so far. I spent all night and then at wake up this morning on it. But it gives an odd "H" on the page. The database is connecting. I'm trying to get it to search the field "Description" for any text links (page or email) and make them clickable when displayed (or is it more proper to call it Echo'd?)
I apologize for my lack of understanding. I'm working hard on it but so very new. I'm getting nervous though cause I'm running out of time to get this working.

The page you can see the results at is (set up just to get something working): http://www.inthisreview.com/bandsreview/viewbands.php

CODE:

$query = "SELECT * FROM band ORDER BY Name ASC";
$result = mysql_query($query);
while($band = mysql_fetch_array($result)) {




$band = preg_replace("/(?<!http:\/\/)(?:^|\b)(((www\.))([\w\.]+)([,:%#&\/?~=\w+\.-]+))(?:\b|$)/is","<a href=http:\/\$1 target=\"_blank\">$1</a>", $band['Description']);
$band = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\" target=\"_blank\">\\0</a>",$band);
$band = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $band);



echo "<p>" . "<b>" . $band['Name'] . "</b>";
echo "<br>" . $band['website'] . "</br>";
echo "<br>" . $band['Description'] . "</br>";

echo "<a href=\"modifybands.php?id=" . $band['id'] . "\"> Modify Review</a>";
echo "<span> </span>";
echo "<a href=\"deletebands.php?id=" . $band['id'] . "\"> Delete Review</a>";

echo "<a href=\"modifybands.php?id=" . $band['id'] . "\"> Modify Review</a>";
echo "<span> </span>";
echo "<a href=\"deletebands.php?id=" . $band['id'] . "\"> Delete Review</a>";

UPDATE....

 

Okay, I got it KINDA working. BUT... it's adding the original website before the link. How can I get it to not do that? I don't think it's auto adding the http first?

 

<?php
//connection to database
}
echo 'Connected successfully';
mysql_select_db(bands);



$query = "SELECT * FROM band ORDER BY Name ASC";
$res = mysql_query($query);
while ($band = mysql_fetch_assoc($res))
{



$news = preg_replace("/(?<!http:\/\/)(?:^|\b)(((www\.))([\w\.]+)([,:%#&\/?~=\w+\.-]+))(?:\b|$)/is","<a href=http:\/\$1 target=\"_blank\">$1</a>", $band['Description']);
$news = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\\0\" target=\"_blank\">\\0</a>",$news);
$news = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",  "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $news);



$addy = preg_replace("/(?<!http:\/\/)(?:^|\b)(((www\.))([\w\.]+)([,:%#&\/?~=\w+\.-]+))(?:\b|$)/is","<a href=http:\/\$1 target=\"_blank\">$1</a>", $band['website']);
$addy = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\\0\" target=\"_blank\">\\0</a>",$addy);
$addy = eregi_replace( "(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",  "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $addy);











       echo "<p>" . "<b>" . $band['Name'] . "</b>";
       echo "<br>" . $addy . "</br>";
       echo "<br>" . $news . "</br>";

       echo "<a href=\"modifybands.php?id=" . $band['id'] . "\"> Modify Review</a>";
       echo "<span> </span>";
       echo "<a href=\"deletebands.php?id=" . $band['id'] . "\"> Delete Review</a>";






}
?>




 

Do realize there is no perfect solution for this. It simple to write code to detect a web address such as: http://domain.com, https://subdomain.domain.com, or www.domain.com. But, it is altogether more difficult if the http(s):// is left off. We usually see web addresses starting with 'www', but that is not required. And, we could probably sniff out address that end with '.com', '.net' etc. such as subdomain.domain.net. But, it gets much harder since you have to hard-code for the TLDs (.com, .net, etc.) - there are hundreds (if not thousands of them)

 

As humans we "know" when someone meant for a piece of text to be a URL (e.g. check out the site warriors.army.mil ). But, it is another thing to write code to figure that out. So, it all depends on how much time and effort you are willing to devote to this.

 

Having said all that, you should create a function to do this since you want to run it on different pieces of text. Try:

 

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

while ($band = mysql_fetch_assoc($res))
{
    $link = makeLinks($band['website']);
    $description = makeLinks($band['Description']);

    echo "<p><b>{$band['Name']}</b><br>";
    echo "{$link}</br>";
    echo "{$description}</br><br>";
    echo "<a href=\"modifybands.php?id={$band['id']}\"> Modify Review</a>";
    echo "<span> </span>";
    echo "<a href=\"deletebands.php?id={$band['id']}\"> Delete Review</a>";
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.