replacing chatacters in a string

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
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

replacing chatacters in a string

Post 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
Last edited by RedRasper on Tue Jul 08, 2003 4:52 am, edited 2 times in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

eregi_replace()

Example:

Code: Select all

<?php
$characters = "hey";
$replacewith = "hello";
$yourstring = eregi_replace($character, $replacewith, $yourstring);
?>
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post by RedRasper »

Many Thanks!! That function is perfect!

Cheers again

RedRasper
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post 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
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Maybe stripslashes will do the trick?
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post by RedRasper »

Thanks, worked a treat! Bit annoyed I didn't think of that!

Thanks

RedRasper
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post 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. =)
Post Reply