replacing URLs with links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

replacing URLs with links

Post by Nay »

how can i get php to replace url's with links?

like how phpbb does. http://www.yahoo.com automaticly turns into a link when i post it.

and do i execute the script when i insert into the database or when i read from the database?

thanks...

-Nay
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Here ya go, a nice little function.

Code: Select all

function text_to_links ($data='') {
        if(empty($data)) { return $data; }

        $lines = split(" ",$data);
        while ( list ($key,$line) = each ($lines)) {
                $line = eregi_replace("(ї ]|^)www.","http://www.",$line);
                $text = eregi_replace("(http://ї^ ) ]+)","<A href="1" target="_blank">1</A>",$line);
                $text = eregi_replace("(ї-a-z0-9_]+(.ї_a-z0-9-]+)*@(їa-z0-9-]+(.їa-z0-9-]+)+))","<AHREF="1",$text'>mailto:1">1</A>",$text);
                $newText .= $text;
        }
        return $newText;
}

So, to use this you would simply do this:

Code: Select all

$text = <text from a textbox, or textarea, or something>

$test = text_to_links($text);
Please note, this is not my code, it is care of jason at PHPComplete.com - so thank him, not me

Here's the link http://www.phpcomplete.com/tutorials.ph ... adTutorial
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Code: Select all

<?
function text_to_links ($data='') &#123;
        if(empty($data)) &#123; return $data; &#125;

        $lines = split(" ",$data);
        while ( list ($key,$line) = each ($lines)) &#123;
                $line = eregi_replace("(&#1111; ]|^)www.","http://www.",$line);
                $text = eregi_replace("(http://&#1111;^ ) ]+)","<A href="$line" target="_blank">$line</A>",$line);
                $text = eregi_replace("(&#1111;-a-z0-9_]+(.&#1111;_a-z0-9-]+)*@(&#1111;a-z0-9-]+(.&#1111;a-z0-9-]+)+))","<a href="mailto:$line">$line</A>",$text);
                $newText .= $text;
        &#125;
        return $newText;
&#125;

$text = $summary;

$test = text_to_links($text);
echo $test;
?>
okay, i did a little of modifying here and there since the code didn't work for me the first time. I got a unexpected T_VARIABLE or something. the code works but now, it removes all my spacing and line breaks. and one more problem is that it has to be a space after the URL. eg:

http://www.yahoo.com<br>

becomes

<a href="http://www.yahoo.com<br>" target="_blank">http://www.yahoo.com<br></a>

-Nay
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

okay.......nevermind replacing the URL(s) with links....

thanks for your help anyways.

i'd be greatly appriciated if someone could help me/write me a bbcode function. is should replace

Code: Select all

&#1111;b]text&#1111;/b]
&#1111;u]text&#1111;/u]
&#1111;i]text&#1111;/i]
&#1111;ur]http://url.com&#1111;/url]
&#1111;url=http://url.com]text&#1111;/url]
thanks.....

it's for the news section in my admin cp which i wrote - with my limited knowledge of php :roll:.

it's still in development but u can check it out at:

http://www.sinsrevival.com/adminpanel/
user: demo
pass: demo

-Nay
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Why not get the code for PHPBB and see how they do it... That's how I started when I needed to write my own replacing script.

Mac
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i just used preg calls....


now if the freakin thing would insert into the db like it's told to and upload like it's told to, then i'd be able to move on to the forums. i use that code with comments on users and in rofiles when they update the bio
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

problem solved......

i used:

Code: Select all

function addLinks($string) {
 $string = preg_replace(
      "/(?<!quot;|[="]|:\/\/)\b((\w+:\/\/|www\.).+?)".
     "(?=\W*([<>\s]|$))/i", "<a href="$1" target="_blank">$1</a>", $string);
 return preg_replace("/href="www/i", "href="http://www", $string);}

$news2 = addLinks($news2);
i looked around the php.net documnetation :P. now i've got a new problem. one i can't really figure out........

i have:

Code: Select all

<form name="news" method="post" action="functions.php?action=edit_news&from=<? echo "$table"; ?>&id=<? echo "$id"; ?>">
<table cellpadding="5" cellspacing="0" border="0" align="left" width="300">

<tr>
<td align="right" valign="top" width="100">Title:</td>
<td align="right" valign="top">
<input type="text" name="edit_title" class="input_type_1" style="width:200" value="<? echo "$title"; ?>"></td>
</tr>

<tr>
<td align="right" valign="top" width="100">News:</td>
<td align="right" valign="top">
<textarea style="width:200;height:150;" class="input_type_1" name="edit_news"><? echo "$news"; ?></textarea>
</td>
</tr>

<tr>
<td align="right" valign="top" colspan="2">
<input type="submit" value="Submit" class="input_type_1">
</td>
</tr>

</table>
</form>
which is to edit the posts in my mysql database......with (functions.php):

Code: Select all

elseif($do=="edit_news") {

$table=$_GET['from'];
$id=$_GET['id'];

$connection = mysql_connect("$hostname" , "$user" , "$pass");
$db = mysql_select_db($dbase , $connection);

$q="UPDATE `$table` SET title = '$edit_title' AND news = '$edit_news' WHERE `id` = '$id'";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());

if($result) {
header("Location: panel.php?edit=news");
}

}
.......................it doesn't work <_<. no errors. It just doesn't change anything in the database. i've gone through it 100000 times. all the names are correct.

help!

this is the final step for my CP, then it's finished! :D

-Nay

edit: my ? >(s) don't display in the code O_o
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

this is quick and not tested in anyway


$sql = "update $table set title = \"$edit_title\", news = \"$edit_news\" where id = \"$id\"";
$connection = mysql_connect("localhost", "username", "password") or die ("your error message");
$db = mysql_select_db("databasenamehere", $connection) or die ("no 2");
$sql_result = mysql_query($sql, $connection) or die ("no 3");
if (!$sql_result) {
echo "<P>Couldn't modify record!";
} else {

it worked code down here.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

hai? O_o

that worked...............well, of course it worked - it wasn't my code <_<.

well, thanks for the help. anyone can point out what was wrong with my code? thanks...

-Nay
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

sorry nay, i can't follow yors well enough and am really relearning it myself soo....all i can think of is that you miss the die on the db connection and you weren't getting that
Post Reply