Page 1 of 1
replacing text in user input.
Posted: Thu Jan 15, 2004 5:19 am
by almostsane
ok what i wanna do is to take a user-inputed plock off text, and before writing it to the DB, check for a certain combo of characters and replace them
eg...
i want it to replace
with
does anyone know how to do this
Posted: Thu Jan 15, 2004 6:33 am
by twigletmac
[php_man]str_replace[/php_man]()
Mac
Posted: Thu Jan 15, 2004 7:55 am
by Dr Evil
You can also try explode():
http://ch2.php.net/manual/en/function.explode.php to split the text using ':' as a string separator.
Then all you need to do is reassemble them as you prefer with a simple concatenation (ie part[1]
."text"
.part[2])
Dr Evil
Posted: Thu Jan 15, 2004 8:17 am
by almostsane
for some reason, str_replace will not work
Code: Select all
<?
$NetCode = array("[Link:","#", "]");
$Actual = array("< a href = ",">","</a>");
$string = "a link will be like this [Link: index.php # Home ] ";
$new = str_replace( $NetCode, $Actual, $string);
echo $new;
?>
still comes out as
a link will be like this [Link: index.php # Home ]
any ideas why this will not work
??
Posted: Thu Jan 15, 2004 8:45 am
by Dr Evil
str_replace only accepts arrays as from PHP 4.0.5
http://ch2.php.net/str_replace
Could this be your problem ?
Dr Evil
Posted: Thu Jan 15, 2004 8:49 am
by m3mn0n
Sami wrote:For syntax highlighting use [php_man]highlight_string[/php_man]()
For your BBcode/Smilies needs:
Code: Select all
<?php
$replaceArray = Array(
// example links
"Google"=>'<a href="http://google.com" target="_blank">Google</a>',
"MSN"=>'<a href="http://msn.com" target="_blank">msn</a>');
foreach ($replaceArray as $old => $new)
{
$thestring = str_replace($old, $new, $thestring);
}
?>
Reference material: [php_man]foreach[/php_man](), [php_man]str_replace[/php_man]()
Posted: Thu Jan 15, 2004 9:24 am
by Dr Evil
almostsane wrote:
Code: Select all
<?
$NetCode = array("[Link:","#", "]");
$Actual = array("< a href = ",">","</a>");
$string = "a link will be like this [Link: index.php # Home ] ";
$new = str_replace( $NetCode, $Actual, $string);
echo $new;
?>
This works with me but you are missing the quotes around the URL and have some spaces too many for html. This works fine:
Code: Select all
<?
$NetCode = array("[Link:","#", "]");
$Actual = array("<a href="","">","</a>");
$string = "a link will be like this [Link: index.php # Home ]";
$new = str_replace( $NetCode, $Actual, $string);
echo $new;
?>
Posted: Thu Jan 15, 2004 8:17 pm
by almostsane
yeah.. the problem is it is still displaying the $NetCode when echo'd
i'll check what version of PHP i'm running that may be it