replacing text in user input.

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
almostsane
Forum Newbie
Posts: 13
Joined: Sat Jan 10, 2004 3:26 am

replacing text in user input.

Post 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

Code: Select all

:Url:news.php:news:
with

Code: Select all

<a href = news.php >news</a>
does anyone know how to do this
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[php_man]str_replace[/php_man]()

Mac
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post 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
almostsane
Forum Newbie
Posts: 13
Joined: Sat Jan 10, 2004 3:26 am

Post 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
??
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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]()
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post 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; 
?>
almostsane
Forum Newbie
Posts: 13
Joined: Sat Jan 10, 2004 3:26 am

Post 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
Post Reply