Page 1 of 1
replacing URLs with links
Posted: Thu Aug 14, 2003 11:13 am
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
Posted: Thu Aug 14, 2003 2:58 pm
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
Posted: Thu Aug 14, 2003 9:11 pm
by Nay
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="$line" target="_blank">$line</A>",$line);
$text = eregi_replace("(ї-a-z0-9_]+(.ї_a-z0-9-]+)*@(їa-z0-9-]+(.їa-z0-9-]+)+))","<a href="mailto:$line">$line</A>",$text);
$newText .= $text;
}
return $newText;
}
$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
Posted: Fri Aug 15, 2003 8:24 am
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
їb]textї/b]
їu]textї/u]
їi]textї/i]
їur]http://url.comї/url]
їurl=http://url.com]textї/url]
thanks.....
it's for the news section in my admin cp which i wrote - with my limited knowledge of php

.
it's still in development but u can check it out at:
http://www.sinsrevival.com/adminpanel/
user: demo
pass: demo
-Nay
Posted: Fri Aug 15, 2003 8:25 am
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
Posted: Fri Aug 15, 2003 9:09 am
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
Posted: Fri Aug 15, 2003 10:59 am
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

. 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!
-Nay
edit: my ? >(s) don't display in the code O_o
Posted: Fri Aug 15, 2003 12:17 pm
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.
Posted: Fri Aug 15, 2003 10:30 pm
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
Posted: Sat Aug 16, 2003 2:25 pm
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