replacing URLs with links
Moderator: General Moderators
replacing URLs with links
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
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
Here ya go, a nice little function.
So, to use this you would simply do this:
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
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);Here's the link http://www.phpcomplete.com/tutorials.ph ... adTutorial
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;
?>http://www.yahoo.com<br>
becomes
<a href="http://www.yahoo.com<br>" target="_blank">http://www.yahoo.com<br></a>
-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
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
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]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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
problem solved......
i used:
i looked around the php.net documnetation
. now i've got a new problem. one i can't really figure out........
i have:
which is to edit the posts in my mysql database......with (functions.php):
.......................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
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 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>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");
}
}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
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.
$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.