str_replace parameter count

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
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

str_replace parameter count

Post by Galahad »

Ok, I'm running PHP 4.3.4. I'm trying to get str_replace working with 4 parameters, but it keeps complaining. This is from the manual:
mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count])
Here's the code I'm trying to use (from the manual, also):

Code: Select all

<?php
$str = str_replace("ll", "", "good golly miss molly!", $count);
?>
However, I always get the following error:
Warning: Wrong parameter count for str_replace()
Any ideas why? Am I just being stupid?

What I'm trying to do ultimately is eliminate a few chars (like ', ", and *) from a string and be able to tell if it found and replaced any of those chars. Is there a better way to do this?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

The optional count arguement is only available in PHP version 5.0.0 or higher.
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

Oh, now I see that in the manual. That's disappointing...

Any ideas how I can do this another way? Should I just use regular expressions to look for any one of the bad characters, or is there an easier (I struggle with reg. expresssions) way to do it? Thanks for the help.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

what are all the characters you looking to find?

Code: Select all

<?php
function illchar($string) { 
  $find[]  = '/\|\|/';    //this would find ||
  $replace[] = 'bad char'; 

  $find[]  = '/\*/';    //this would find *
  $replace[] = 'bad char'; 

foreach($find as $key => $value) { 
        $string = preg_replace($value, $replace[$key], $string); 
    } 

    return $string; 
} 
?>
Just add find[] vars to whatver u wish to find and make the replace[] var whatever u wish to replace the find value with.

any help about reg exp about some characters u can come back. i'm sure red would be able to assist you if someone else can not.

and u can call the function with like

$text = illchar($textvar);
echo $text;
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

There are a couple of different ways to do it dependant on exactly what you need to acheive....

you could use str_replace then compare the new string with the old string to see if anything was replaced....

Code: Select all

$search = array('''', '"', '*');
$old_string = 'Here is "some" string**';

$new_string = str_replace($search, '', $old_string);

if ($new_string != $old_string)
{
  // strings are different something has been replaced
}
If you need a count of how many have been replaced.....

Code: Select all

$search_string = 'Here is "a" string **';
$search_chars  = array('/''/', '/"/', '/\*/');
$counter = 0;

function counter()
{
  global $counter;
  $counter++;
  return;
}

$new = preg_replace_callback($search_chars, 'counter', $search_string);

echo $counter;
The value of $counter when echoed should be 4

Unfortunately it gets a bit more complex if you are trying to replace characters with other characters.


Tim
Although your example is one way to do string replacement, you should note that it is not very efficient. If you don't require any complex pattern matching you should always favour a standard str_replace as it should be quicker as it doesn't need to invoke the regex engine.

Also, both str_replace and preg_replace can take arrays as their arguments so you don't need the foreach loop. You could achieve the same thing by doing.....

Code: Select all

$find = array('/\|\|/', '/\*/');
$replace = array('bad char', 'bad char');

$string = preg_replace($find, $replace, $string);
Or the quicker way...

Code: Select all

$find = array('||', '*');
$replace = array('bad char', 'bad char');

$string = str_replace($find, $replace, $string);
Post Reply