string replacement

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
Pain
Forum Newbie
Posts: 8
Joined: Wed Jun 15, 2005 7:42 am
Location: Riga, Latvia

string replacement

Post by Pain »

I have this problem with string replacement... I have a string and I want to leave some symbols in it and all the other symbols replace with "-"... I know that there are functions, that replace all the given symbols, but I need the function to replace all the "not given" symblos. Is there a function like this?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: string replacement

Post by foobar »

Sounds like you need preg_replace(). Without any code it'll be hard to provide any personalised help, though.
Pain
Forum Newbie
Posts: 8
Joined: Wed Jun 15, 2005 7:42 am
Location: Riga, Latvia

Re: string replacement

Post by Pain »

foobar wrote:Sounds like you need preg_replace(). Without any code it'll be hard to provide any personalised help, though.
I think it isnt the right one... I will try to give an example... I have a string and want it to contain only symbols "a", "b" and "-"... So what can I do to replace all other symbols with "-"?
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

str_replace should do the trick

example:

Code: Select all

$replaced_string = str_replace(array("&", "%", "#"), "-", $string);
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$string = preg_replace('/[^ab-]{1}/', '-', $string);
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Why is preg_replace() not correct? You know something we don't? :)

There are no other builtin functions that can accomplish this for you. preg_replace() is your man. Your only other option would be to iterate over the string and test each character as you go, replacing as needed.

Cheers
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

mickd wrote:str_replace should do the trick

example:

Code: Select all

$replaced_string = str_replace(array("&", "%", "#"), "-", $string);
Based on his description of the problem, str_replace() is going to need a search array that's much larger then what you provided. It's not practical by comparison to preg_replace().

Cheers
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

BDKR wrote:
Based on his description of the problem, str_replace() is going to need a search array that's much larger then what you provided. It's not practical by comparison to preg_replace().

Cheers
ah yeah, only realised afterwards that he wanted to keep so few. preg_replace would be better in that case :wink:
Pain
Forum Newbie
Posts: 8
Joined: Wed Jun 15, 2005 7:42 am
Location: Riga, Latvia

Post by Pain »

thanks, I just could not find how I could replace the "incorrest" symbols, but now I know and my wisdom has grown :D
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Pain wrote:thanks, I just could not find how I could replace the "incorrest" symbols, but now I know and my wisdom has grown :D
Example:

Code: Select all

<?php

$input = 'I love apples! 2me1three. <some smurf />';

$bad_boys = '/[\w]+/'; //bad "symbols" are non perl-words
$good_guy = '-';
$string = preg_replace($bad_boys, $good_guy, $input);

echo $string;

$bad_boys = '/[^a-zA-Z0-9]+/'; //non alphanumeric characthers (^ is the negation of everything within [])
$good_guy = '-';
$string = preg_replace($bad_boys, $good_guy, $input);

echo $string;

$bad_boys = '/[\^°@~+#?\]\[\(\)\$\!]+/'; //a bunch of non-alphanumeric characters
$good_guy = '-';
$string = preg_replace($bad_boys, $good_guy, $input);

echo $string;

?>
Post Reply