Page 1 of 1

replacing chatacters in a string

Posted: Tue Jul 08, 2003 3:40 am
by RedRasper
Hi all,

Got a question, I'm sure the answer is really simple, but I can't seem to be able to do it.

Basically I want to take a string and replace any ' in the string with ` . I looked at the str functions and couldn't seem to find anything that would do it.

I wrote this :

Code: Select all

<?php
$test = "i've don't can't `' '` ";


$length = strlen($test);

for($i=0;$i<$length;$i++) 
	if($test[$i] == "'")
		$result[$i] == "`";


?>
Thank you for any help!

RedRasper

Posted: Tue Jul 08, 2003 4:02 am
by m3mn0n
eregi_replace()

Example:

Code: Select all

<?php
$characters = "hey";
$replacewith = "hello";
$yourstring = eregi_replace($character, $replacewith, $yourstring);
?>

Posted: Tue Jul 08, 2003 4:20 am
by RedRasper
Many Thanks!! That function is perfect!

Cheers again

RedRasper

Posted: Tue Jul 08, 2003 4:59 am
by RedRasper
Just having problems with that function.

It works fine on php 4.3.2, but our webserver only has 4.1.1 at the moment (Long story, but its very difficult to upgrade it!)

With 4.1.1 I get the results I want a ' turned into a `, but the problem is I also get the leading \ in it.

So from

won't can't don't

I get

won\`t can\`t don\`t

The code I've used it

Code: Select all

<?php

$replacewith = "`"; 
$character = "'";
$message = eregi_replace($character, $replacewith, $message);
?>
Thanks again

RedRasper

Posted: Tue Jul 08, 2003 5:23 am
by []InTeR[]
Maybe stripslashes will do the trick?

Posted: Tue Jul 08, 2003 5:39 am
by RedRasper
Thanks, worked a treat! Bit annoyed I didn't think of that!

Thanks

RedRasper

Posted: Tue Jul 08, 2003 10:04 pm
by macewan
[]InTeR[] wrote:Maybe stripslashes will do the trick?
Thanks for that suggestion. It trigger a thought that helped me solve a silly problem we were having with something. *vague I know* just know you helped. =)