note 50923 added to function.str-replace
| From: | nielsvandenberge at hotmail dot com | Date: | Mon, 14 Mar 2005 10:12:14 +0000 |
| Subject: | note 50923 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86389@lists.php.net to get a copy of this message | ||
I got a few things to add on the subject Jesse posted about the smiley replacement.
Bad HTML code can be removed with the php function strip_tags().
Further I would like to add my URL- and IMG-replace function. The problem was that it would only
replace one IMG or URL node, but since the functions calls itself until there aren't IMG or
URL-nodes anymore they will all be replaced.
Note that the replace function is case sensitive, so for example [b] won't be replaced.
Regards,
Here's the code:
<?php
function replaceMessage($message) {
$message = strip_tags($message,
'<b></b><i></i><u></u><a></a><img>');
$message = str_replace ("\n", "<BR>", "$message");
// When you store the $message in a database you might get errors cause of the quotes
$message = str_replace("[singleQuote]", "'", $message);
$message = str_replace("[doubleQuote]", "\"", $message);
$message = str_replace ("[U]", "<U>", "$message");
$message = str_replace ("[/U]", "</U>", "$message");
$message = str_replace ("[I]", "<I>", "$message");
$message = str_replace ("[/I]", "</I>", "$message");
$message = str_replace ("[B]", "<B>", "$message");
$message = str_replace ("[/B]", "</B>", "$message");
$message = replaceUrl($message);
$message = replaceImg($message);
return $message;
}
function replaceImg($message) {
// Make image from [img]htp://.... [/img]
if (strpos($message, "[img]")){
$begImg = strpos($message, "[img]");
$endImg = strpos($message, "[/img]");
$img = substr($message, $begImg, $endImg-$begImg+6);
$link = substr($img, 5, $endImg - $begImg -5);
$htmlImg = "<img src=$link border='0'>";
$message = str_replace($img, $htmlImg, $message);
// searches for other [img]-nodes
$message = replaceImg($message);
}
return $message;
}
function replaceUrl($message) {
// Make link from [url]htp://.... [/url] or [url=http://.... ]text[/url]
if (strpos($message, "[url")){
$begUrl = strpos($message, "[url");
$endUrl = strpos($message, "[/url]");
$url = substr($message, $begUrl, $endUrl-$begUrl+6);
$posBracket = strpos($url, "]");
if ($posBracket != null){
if ($posBracket == 4){
// [url]http://.... [/url]
$link = substr($url, 5, $endUrl - $begUrl -5);
$htmlUrl = "<a href=$link target='_blank'>$link</A>";
} else {
// [url=http://....]text[/url]
$link = substr($url, 5, $posBracket-5);
$text = substr($url, $posBracket+1, strpos($url, "[/url]") - $posBracket-1);
$htmlUrl = "<a href=$link target='main'>$text</A>";
}
}
$message = str_replace($url, $htmlUrl, $message);
// searches for other [url]-nodes
$message = replaceUrl($message);
}
return $message;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50923
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50923&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50923&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50923&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50923&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50923&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50923&report=yes
Search -- http://master.php.net/manage/user-notes.php