PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

str_rot13> <str_repeat
Last updated: Fri, 11 Apr 2008

view this page in

str_replace

(PHP 4, PHP 5)

str_replace — Remplace toutes les occurrences dans une chaîne

Description

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

str_replace() retourne une chaîne ou un tableau, dont toutes les occurrences de search dans subject ont été remplacées par replace . Si vous n'avez pas besoin de règles de remplacement compliquées, utilisez cette fonction de préférence à ereg_replace() et preg_replace().

Depuis PHP 4.0.5, chaque paramètre de str_replace() peut être un tableau.

Avertissement

Avant PHP 4.3.3, un bogue existait lors de l'utilisation de tableaux avec les paramètres search et replace en même temps. Les index de search qui étaient vides étaient ignorés, mais le pointeur interne de replace n'étais pas incrémenté. Cela a été corrigé en PHP 4.3.3, tout script s'appuyant sur ce bogue, doit supprimer les entrées vides avant d'appeler cette fonction pour imiter le comportement d'origine.

Si subject est un tableau, alors le remplacement se fera sur chaque élément de celui-ci, et la valeur retournée sera aussi un tableau.

Si search et replace sont des tableaux, alors str_replace() prendra une valeur de chaque tableau, et l'utilisera pour faire le remplacement dans subject . Si replace a moins de valeurs que search , alors une chaîne vide sera utilisée pour effectuer les remplacements. Si search est un tableau et que replace est une chaîne, alors la chaîne de remplacement sera utilisée pour chaque élément de search . Cependant, l'inverse n'aurait aucun sens.

Si search ou replace sont des tableaux, les éléments sont traités du premier, au dernier.

Exemple #1 Exemple avec str_replace()

<?php
// Génère : <body text='black'>
echo $bodytag str_replace("%body%""black""<body text='%body%'>");

// Génère : Bnjr l mnd
$voyelles = array("a""e""i""o""u""A""E""I""O""U");
echo 
$consonnes str_replace($voyelles"""Bonjour le monde");

// Génère : Vous devriez manger des pizzas, des glaces et des gâteaux tous les jours.
$phrase  "Vous devriez manger des fruits, des légumes et des fibres tous les jours.";
$regime = array("fruits""légumes""fibres");
$bonne_chere   = array("pizzas""glaces""gâteaux");

echo 
$newphrase str_replace($regime$bonne_chere$phrase);

// Utilisation du compteur d'occurences en PHP 5.0.0
$str str_replace("a""""La disparition est un lipogramme en E."$count);
echo 
$count// 3

// Ordre des remplacements
$str     "Ligne 1\nLigne 2\rLigne 3\r\nLigne 4\n";
$order   = array("\r\n""\n""\r");
$replace '<br />';
// Traitement du premier \r\n, ils ne seront pas convertis deux fois.
$newstr str_replace($order$replace$str);

// Affiche : apearpearle pear
$letters = array('a''p');
$fruit   = array('apple''pear');
$text    'a p';
$output  str_replace($letters$fruit$text);
echo 
$output;
 
?>

Note: Cette fonction gère les chaînes binaires.

Note: Cette fonction est sensible à la casse. Utilisez la fonction str_ireplace() pour un remplacement insensible à la casse.

Note: Depuis PHP 5.0, le nombre de valeurs de search trouvées et remplacées seront retournées dans le paramètre count passé par référence. Avant PHP 5.0.0, ce paramètre n'est pas disponible.

Voir aussi str_ireplace(), substr_replace(), preg_replace() et strtr().



str_rot13> <str_repeat
Last updated: Fri, 11 Apr 2008
 
add a note add a note User Contributed Notes
str_replace
cclarkson at htcomp dot net
28-Apr-2008 12:32
The example presented by bladescope at googlemail dot com has a couple of syntax errors. This works:

<?php
    $template
= "The {color} {object} is in {location}";
   
$array = array(
       
'{object}'    => 'Ball',
       
'{color}'     => 'Red',
       
'{location}'  => 'The Playground',
    );
    foreach (
$array as $search => $replace) {
       
$template = str_replace($search, $replace, $template);
    }
    print
$template;
?>

I did not check it for speed or thoroughly test it, but this function seems to do the same thing more succinctly.

<?php
   
function template($array, $template) {
        return
str_replace(
           
array_keys($array),
           
array_values($array),
           
$template
       
);
    }

    echo
template(
         array(
           
'{color}'     => 'red',
           
'{object}'    => 'ball',
           
'{location}'  => 'the playground',
        ),
       
'The {color} {object} is in {location}.'
   
);
?>
bladescope at googlemail dot com
16-Apr-2008 04:07
An easier and safer way to use str_replace with arrays without the risk of order messups is by creating an array. The title would be the string to search for, and the value would be the value to replace it with. From there, it's a matter of using a foreach loop. e.g.

<?php
$template
= "The {color} {object} is in {location}";
$array = array(
   
'{object}'    =>    'Ball';
   
'{color}'     =>    'Red';
   
'{location}'  =>    'The Playground';
);
foreach (
$array as $search=>$replace) {
   
$string = str_replace($search, $replace, $template);
}
print
$string; // Returns "The Red Ball is in The Playground".
?>
griffinmt at charter dot net
02-Feb-2008 11:04
It would appear that there is a length restriction in str_replace. When processing a large string (approx 35k long) and trying to replace some binary data with characters, it stopped replacing at about the 16K boundary.
Have not yet worked out an alternative yet, other than splitting the sting into pieces first.
marc at palaueb dot com
24-Sep-2007 10:44
I'm searching for a function who replace the last occurrence of a string but I have not found anything.

Check this out:

function str_rreplace($search,$replace,$subject){
    $strrpos=strrpos($subject,$search);
    return substr_replace($subject,$replace,$strrpos,strlen($search));
}

the only one problem is that do not have a limit to replace, just replace the last occurrence.
David Gimeno i Ayuso (info at sima dot cat)
09-Aug-2007 09:50
This is the functions I wrote for the problem reported above, str_replace in multi-dimensional arrays. It can work with preg_replace as well.

function array_replace($SEARCH,$REPLACE,$INPUT) {
  if (is_array($INPUT) and count($INPUT)<>count($INPUT,1)):
    foreach($INPUT as $FAN):
      $OUTPUT[]=array_replace($SEARCH,$REPLACE,$FAN);
    endforeach;
  else:
    $OUTPUT=str_replace($SEARCH,$REPLACE,$INPUT);
  endif;
  return $OUTPUT;
}
David Gimeno i Ayuso (info at sima dot cat)
09-Aug-2007 09:22
With PHP 4.3.1, at least, str_replace works fine when working with single arrays but mess it all with two or more dimension arrays.

<?php
$subject
= array("You should eat this","this","and this every day.");
$search  = "this";
$replace = "that";
$new     = str_replace($search, $replace, $subject);

print_r($new); // Array ( [0] => You should eat that [1] => that [2] => and that every day. )

echo "<hr />";

$subject = array(array("first", "You should eat this")
                ,array(
"second","this")
                ,array(
"third", "and this every day."));
$search  = "this";
$replace = "that";
$new     = str_replace($search, $replace, $subject);

print_r($new); // Array ( [0] => Array [1] => Array [2] => Array )

?>
mjaque at ilkebenson dot com
22-Jul-2007 10:28
In order to replace carriage return characters from form inputs, if everything else fails, you can try:

<?php
$new_text
= str_replace(chr(13).chr(10), '_', $original_text);
?>

And in order to show the line feeds in a javascript alert, you can do:

<?php
$new_text
= str_replace(chr(13).chr(10), '\n', $original_text);
?>
cm at k-a-p dot com
13-Jun-2007 03:47
Here is a version that allows for empty multidimensional arrays:

function str_replace_array ($search, $replace, $subject) {
    if (is_array($subject)) {
        foreach ($subject as $id=>$inner_subject) {
            $subject[$id]=str_replace_array($search, $replace, $inner_subject);
        }
    } else {
        $subject=str_replace($search, $replace, $subject);
    }
    return $subject;
}
tim at hysniu dot com
05-Jun-2007 08:27
I found that having UTF-8 strings in as argument didnt
work for me using heavyraptors function.
Adding UTF-8 as argument on htmlentities
fixed the problem.

cheers, tim at hysniu.com

<?php
function replace_accents($str) {
 
$str = htmlentities($str, ENT_COMPAT, "UTF-8");
 
$str = preg_replace(
'/&([a-zA-Z])(uml|acute|grave|circ|tilde);/',
'$1',$str);
  return
html_entity_decode($str);
}

?>
kole
26-Feb-2007 02:48
My input is MS Excel file but I want to save ‘,’,“,” as ',',",".

    $badchr        = array(
        "\xc2", // prefix 1
        "\x80", // prefix 2
        "\x98", // single quote opening
        "\x99", // single quote closing
        "\x8c", // double quote opening
        "\x9d"  // double quote closing
    );
       
    $goodchr    = array('', '', '\'', '\'', '"', '"');
       
    str_replace($badchr, $goodchr, $strFromExcelFile);

Works for me.
rlee0001 at sbcglobal dot net
16-Feb-2007 09:30
This is a more rigid alternative to spectrereturns at creaturestoke dot com's replace_different function:

<?php

       
function str_replace_many ($search, $replacements, $subject) {
           
$index = strlen($subject);
           
$replacements = array_reverse($replacements);

            if (
count($replacements) != substr_count($subject, $search)) {
                return
FALSE;
            }

            foreach (
$replacements as $replacement) {
               
$index = strrpos(substr($subject, 0, $index), $search);
               
$prefix = substr($subject, 0, $index);
               
$suffix = substr($subject, $index + 1);
               
$subject = $prefix . $replacement . $suffix;
            }

            return
$subject;
        }
?>

This will return false if there are a different number of $replacements versus number of occurrences of $search in $subject. Additionally, $search much be exactly one character (if a string is provided, only the first character in the string will be used). Examples:

<?php
       
echo str_replace_many('?',array('Jane','banana'),'? is eating a ?.');
?>

prints: "Jane is eating a banana."
juli at poblenou dot com
14-Feb-2007 11:56
The function works with arrays (as stated) provided those are unidimensional arrays.
In case you use multidimensional arrays, it won't replace anything, at least in PHP 4.4. If you need to work with multidimensional arrays, you should iterate (using foreach) over every unidimensional array.
15-Jan-2007 10:42
Before spending hours searching your application why it makes UTF-8 encoding into some malformed something with str_replace, make sure you save your PHP file in UTF-8 (NO BOM).

This was at least one of my problems.
Boris DOT Christ AT laposte DOT net
27-Nov-2006 10:32
I create a little function to transform (to example) "User@example.net" in "user AT example DOT net" and conversely.

<?php
function code_mail($email) {
    if(
preg_match('`^.+@.+\..{1,5}$`', $email)) { //email format
       
$email = str_replace('.', ' DOT ', $email); //replace . by dot
       
$email = str_replace('@', ' AT ', $email); //replace @ by at
       
return $email;
    }       
    else {
//not email format
       
return false;
    }
}
function
decode_mail($email) { //on décode...
   
$email = str_replace(' DOT ', '.', $email); //replace dot by .
   
$email = str_replace(' AT ', '@', $email); //replace at by @
   
return $email;
}
?>
Ota L.
11-Nov-2006 12:14
This simple function may be usefull to convert czech text from cp-1250 encoding to iso-8859-2 (convert S, T and Z with caron).

I use it because mbstring does not support windows-1250 (i.e. mb_convert_encoding(win2iso($text), "utf-8", "iso-8859-2");).

<?php
function win2iso($text)
{
   
$win = array ("\x9A", "\x9D", "\x9E", "\x8A", "\x8D", "\x8E");
   
$iso = array ("\xB9", "\xBB", "\xBE", "\xA9", "\xAB", "\xAE");
    return
str_replace($win, $iso, $text);
}
?>
webmaster at unitedscripters dot com
30-Jul-2006 03:50
You may have decided to save in a non ANSI format a file so that a few fancy chars that you plan to replace can be viewed by your human eyes too (aren't all those empty rectangles a curse?).

Fine. All works just fine in the file, and all the replacements occur as intended.
You make a class out of those codes.

Then you put this non ANSI encoded file in your includes folder. Isn't it a nice class?

Well, don't call in such class as an include into another file, if the latter is ANSI : the char look up tables will NOT match, and the latter file (say the caller) will (I guess) feed ANSI codes to the included file (say the called) and you will spend a day wondering why the class methods work 100% well when you perform replacements directly from within the called, and yet the very same methods, even with the very same copied-and-pasted examples, fail miserably when performed from within the caller.
A very "stimulating" debugging!

I just came out from a night spent on this. I just forgot the included class file wasn't saved as an ANSI.

<?php
class regexp{
//blah blah...
var $toascii=array(    'Ã' => 'A');
//blah blah
function toascii_replace($input, $addSlashes=0){
return (!
$addSlashes)? strtr($input, $this->toascii): addslashes( strtr($input, $this->toascii) );
}
}

$r=new regexp();
$input='Ã';
print
$r->toascii_replace($input);//prints A
?>

Now save that class (remove the print statement too) in a format that isn't ANSI, say UTF-8.
Then do:

<?php
include_once('regexp.php');

$r=new regexp();
$input='Ã';
print
$r->toascii_replace($input);//prints... Ã
?>

IDENTICAL codes, different results.

note: the class uses strtr but would happen with all replacing oriented functions, and I can pester all the documentations. Maybe I worked out the wrong reason, but the behaviour occurs.
matt wheaton
30-Mar-2006 05:40
As an effort to remove those Word copy and paste smart quotes, I've found that this works with UTF8 encoded strings (where $text in the following example is UTF8). Also the elipsis and em and en dashes are replaced.

There is an "invisible" character after the †for the right side double smart quote that doesn't seem to display here. It is chr(157).

<?php
  $find
[] = '“'// left side double smart quote
 
$find[] = '”'// right side double smart quote
 
$find[] = '‘'// left side single smart quote
 
$find[] = '’'// right side single smart quote
 
$find[] = '…'// elipsis
 
$find[] = '—'// em dash
 
$find[] = '–'// en dash

 
$replace[] = '"';
 
$replace[] = '"';
 
$replace[] = "'";
 
$replace[] = "'";
 
$replace[] = "...";
 
$replace[] = "-";
 
$replace[] = "-";

 
$text = str_replace($find, $replace, $text);
?>
vlatko dot surlan at RMVME gmail dot com
30-Jan-2006 06:57
Suggested code has some drawbacks in the context of many replacement pairs (i am not suggesting that mine has none). The example code you provided:

<?php
return str_replace(
  array(
'&#262;', '&#263;', '&#264;', [...]),
  array(
'&#262;', '&#263;', '&#264;', [...]),
 
strtoupper($string)
 ) ;
?>

relies on the fact that visual identation is provided by all replacement elements beeing of the same width, which is hardly ever true in the real world (please correct me if I'm wrong). Another nasty feature is that this code extends to the right with addition of new elements while the solution provided in the earlier post extends in the driection of the natural flow of code, thus, retaining correct code width for all cases. Perhaps you have ommited the fact that I specifically intended suggested code format for cases with many replacement pairs indicated with /* many lines here */ in the previous post?
hermes at andycostell dot com
11-Dec-2004 08:57
A simple function to take out the double line breaks "%0D%0A" from a string made from text being wrapped in a textarea and turn them into single line breaks.

<?php
function remove_extra_linebreaks($string) {
     
$new_string=urlencode ($string);
   
$new_string=ereg_replace("%0D", " ", $new_string);
   
$new_string=urldecode  ($new_string);
  return
$new_string;
}
?>

I use it when taking text from a textarea with wrap="hard" that I'm emailing out on the next page, for instance. Otherwise there's an extra empty line between each line in the emailed text, which looks nasty!
thewolf at pixelcarnage dot com
23-Oct-2003 04:38
I got sick of trying to replace just a word, so I decided I would write my own string replacement code. When that code because far to big and a little faulty I decided to use a simple preg_replace:

<?php
/**
 * Written by Rowan Lewis of PixelCarnage.com
 * $search(string), the string to be searched for
 * $replace(string), the string to replace $search
 * $subject(string), the string to be searched in
 */
function word_replace($search, $replace, $subject) {
    return
preg_replace('/[a-zA-Z]+/e', '\'\0\' == \'' . $search . '\' ? \'' . $replace . '\': \'\0\';', $subject);
}
?>

I hope that this code helpes someone!
David Gimeno i Ayuso (info at sima-pc dot com)
25-Aug-2003 03:12
Take care with order when using arrays in replacement.

<?php
$match
=array("ONE","TWO","THREE");
$replace=array("TWO WORDS","MANY LETTERS","OTHER IDEAS");
$sample="ONE SAMPLE";
echo
str_replace($match,$replace,$sample);
?>

It will show: "MANY LETTERS WORDS SAMPLE"

That is, after replacing "ONE" with "TWO WORDS", process follows with next array item and it changes "TWO" with "MANY LETTERS".
imho at auspantheon dot com
27-Jun-2003 01:08
An excellent way of making sure your pages don't contain "invalid entities", IE. Valid HTML is using the following function.

Most forum packages / extensions provide output containing the symbol &, we don't want this!

<?php
function include_safe ($file)
{
   
$array = file($file);

    foreach (
$array as $line) {
    print
str_replace('&', '&amp;', $line);
    }
}
?>

The same technique could also be used in conjuntion with htmlmspecialchars or htmlentities.
13-Jun-2003 12:59
Having a string for $search and an array for $replace won't work. This is mentioned on the preg_replace() page where it's described as not making sense, but not here.

But one could interpret such a situation and hence implement str_replace so that a signature of (string, array, string) or even (string, array, array) would "work". The result of

<?php
$search
= '*';
$replace = range(1,20);
$subject = '{*}';
$result = str_replace($search, $replace, $subject);
?>

could be an array of elements "{1}", "{2}", "{3}" .... In other words it would have the same effect as

<?php
$search
= '*';
$replace = range(1,20);
$subject = '{*}';
$result = array();
foreach(
$replace as $r) {
   
$result[] = str_replace($search, $r, $subject);
}
?>

I leave more elaborate applications to your imagination :)

The result of a str_replace(string,array,array) would therefore presumably be an array of arrays, with its dimensions indexed by the two arrays passed in. But then there's the question of which is the first dimension and which is the second.
rit at NOSPAMchatol dot com
07-Jan-2003 01:32
I was trying to remove newlines from a textarea input (result of failed submission of parent form - JS verification not possible in this situation) to send back to the textarea via javascript (due to the fact that setting the value in the textarea tag does not work) and had a hard time figuring it out.

If anyone cares, try replacing: "%0D%0A" which is how I found it(changed my POST method to GET) and tracked it down in the address bar of my browser. Hope this helps, I sure wish someone else had posted it earlier!
art at zollerwagner dot com
10-Oct-2002 12:26
To use one or two arrays in str_replace,

this appears to be the correct format:
<?php
str_replace
(array('1st_current_needle_element', '2nd', '3rd'), array('1st_new_needle_element', '2nd', '3rd'), $haystack)
?>

Example of a single array, which simply removes the characters in the first array:
<?php
$text
=str_replace(array('<', '>', '\\', '/', '='), "", $text);
?>

This will delete the chars: < > \ / =

It could also be done by first defining the array(s), like this:
<?php
$targetChars
=array('<', '>', '\\', '/', '=');
$text=str_replace($targetChars, "", $text);
?>

str_rot13> <str_repeat
Last updated: Fri, 11 Apr 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites