Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : str_replace

str_replace

Replace all occurrences of the search string with the replacement string (PHP 4, PHP 5)
mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )

Example 2446. str_replace() examples

<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo
$count; // 2

// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);

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

Related Examples ( Source code ) » str_replace










Code Examples / Notes » str_replace

webmaster

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.


patrick

With reference to the google type searches above ...
This is a case insensitive replace....
This code below will highlight the "found" text in red and return the string unaltered (in terms of case).
<?php
function turn_red($haystack,$needle)
{
    $h=strtoupper($haystack);
$n=strtoupper($needle);
$pos=strpos($h,$n);
if ($pos !== false)
{
$var=substr($haystack,0,$pos)."<font color='red'>".substr($haystack,$pos,strlen($needle))."</font>";
$var.=substr($haystack,($pos+strlen($needle)));
$haystack=$var;
}
return $haystack;
}
?>


david gimeno i ayuso info

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 )
?>


jr

When the first argument to str_replace() is an array, the elements are checked in increasing order of array index, as one would expect.
For example,
str_replace (array ('ABC', 'BC'), array ('E', 'F'), 'ABC')
returns 'E';
str_replace (array ('BC', 'ABC'), array ('E', 'F'), 'ABC')
returns 'AE'.


art

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);
?>


ota l.

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);
}
?>


david gimeno i ayuso info

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;
}


joaquin

This is an another accent remover which converts foreign characters to their English equivilent. It converts any  character's to their HTML value and then uses preg_replace to find the desired value.  The function is based on a earlier entry: replace_accents.
function remove_accents( $string )
{
$string = htmlentities($string);
return preg_replace("/&([a-z])[a-z]+;/i","$1",$string);
}
example:
$string = "êéçýCÉÇÀ";
echo remove_accents( $string );
// will output: eecyCECA


rlee0001

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."


spectrereturns

This is a little function I wrote replaces substrings in a string with different values. If there are more instances of $search in $string then there are elements in $replace, it will start over.
<?php
 function replace_different($search,$replace,$string) {
   $occs = substr_count($string,$search);
   $last = 0;
   $cur = 0;
   $data = '';
   for ($i=0;$i<$occs;$i++) {
     $find = strpos($string,$search,$last);
     $data .= substr($string,$last,$find-$last).$replace[$cur];
     $last = $find+strlen($search);
     if (++$cur == count($replace)) {
       $cur = 0;
     }
   }
   return $data.substr($string,$last);
 }
?>
Example:
<?php
$string = '`atext`,`btext`';
echo replace_different('`',array('0~`','1~`'),$string);
?>
Will return:
0~`atext1~`,0~`btext1~`


dmitry fedotov box

This function replase in string
<?php
function ReplaceOne($in, $out, $content){
if ($pos = strpos($content, $in)){
return substr($content, 0, $pos) . $out . substr($content, $pos+strlen($in));
} else {
return $content;
}
}
$string = "TEXT bla bla bla TEXT bla bla TEXT";
echo str_replace("TEXT", "1", $string); // 1 bla bla bla 1 bla bla 1
$content = ReplaceOne("TEXT", "1", $content); // 1 bla bla bla TEXT bla bla TEXT
$content = ReplaceOne("TEXT", "2", $content); // 1 bla bla bla 2 bla bla TEXT
$content = ReplaceOne("TEXT", "3", $content); // 1 bla bla bla 2 bla bla 3
// etc...
?>


will

This function is an example of replacing month numbers with the actual textual value.  
<?php
function createMonths($month_start,$month_end) {
$month = array("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12");
$month_replace = array("January","February","March","April","May","June",
"July","August","September","October","November", "December");
$x_end = $month_end + 1;
// Add one to the value of the end month
for ($x = $month_start; $x < $x_end; $x++) {
$new_months .= $x.",";
// Count from the begin month to the end month
}
$new_months = substr($new_months,0,-1);
// Cut the last comma off the end month
$newmonth = explode(",", $new_months);
// Place each int month into an array
$newsmonth = str_replace($month,$month_replace,$newmonth);
// Replace the int month with a textual string
var_dump ($newsmonth);
// Dump the data
}
createMonths(1,3);
/* Will print array(3)
{ [0]=> string(7) "January" [1]=> string(8) "February" [2]=> string(5) "March" } */
?>


jmack

The ReplaceOne function above does not work properly in all cases. (Not even using the provided example.)
Here is my replacement:
function str_replace_once($search, $replace, $subject)
{
   if(($pos = strpos($subject, $search)) !== false)
{
       $ret = substr($subject, 0, $pos).$replace.substr($subject, $pos + strlen($search));
   }
else
{
       $ret = $subject;
   }
   
   return($ret);
}


unleadedis

The problem is that str_replace seems to call the replace even though there may not be a replacement.
This is a problem in that in my parsing script when it finds a certain tag it calls a function, this function does a few SQL queries. It has been noticed that doing a str_replace on a page that contains no 'tag' that I am searching for, the function is called anyway!!!
To get around this I have to do a str_pos to see if it exists then call str_replace.
eg,
<?php
// Breadcrumb trail (same as per sitemap)
$bc_pos = strpos($string,"[[breadcrumbtrail]]");
if ($bc_pos || is_numeric($bc_pos)) { // true ? then exectue!
   $string = str_replace ("[[breadcrumbtrail]]",
        generate_breadcrumb($nodedata_arr, $db), $string);
}
?>


juli

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.


guy gordon

The exact meaning of "all occurrences" should be documented.  Str_replace does not include any replaced text in subsequent searches.    (The search for the next occurrence starts after the replace, not at the beginning of the string.)  This can lead to the result string retaining occurrences of the search string.
For example, if you want to replace all multiple spaces with a single space, a simple str_replace will not work:
 $strIn = '      ';     //six spaces
 echo str_replace('  ',' ',$strIn);  //will echo three spaces
Instead, you could use:
 while (strpos($strIn,'  ') !== False)
   $strIn = str_replace('  ',' ',$strIn);


david gimeno i ayuso info

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".


vlatko dot surlan

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?


gomodo

Some controls problems when you want to replace backslashes '\' ?
Look this example and the protected functions (we try to replace '\' by '.') :
<?php
//****** test with bad double-quote parameter
test_replace_backslash("abcde\2fghij",".");
//******
echo "\n";
//****** test with good simple-quote parameter
test_replace_backslash('abcde\2fghij',".");
//**************************************
//************ functions ****************
function test_replace_backslash($str_test,$str_replace)
{
$str_new= replace_backslash($str_test,".");
if (!$str_new) echo "no readable char found - control call with 'string' and not ".'"'."string".'"';
else echo $str_new;
}
//*****
function replace_backslash($str_to_detect,$str_replace)
{
if(chrs_is_all_readable($str_to_detect)) {
return(preg_replace('[\\\]',$str_replace,$str_to_detect));
}
return (false);
}
//***** inspired from http://fr.php.net/manual/fr/function.chr.php#70931
//***** ( thank to "jgraef at users dot sf dot net")
function chrs_is_all_readable($string)
{
 for ($i=0;$i<strlen($string);$i++)
{
$chr = $string{$i};
$ord = ord($chr);
if ($ord<32 or $ord>126) return(false);
}
 return (true);
}
?>
this example return :
no readable char found - control call with 'string' and not "string"
abcde.2fghij


hinom

recursive e case-insensitive str_replace for php users under 5 version
stripos() function is available in php5, therefore needed create generic function stripos2()
hope this help many users
best regards for all PHP!
thnks for zend and php developers
<?php
$find   = "script"; // string to search
$new    = "##";     // string to replace
// text
$string = "33onSCRIPTon oano AoKO k SCRIPT--atooto Atoto SCRIPTmoomo lko KOx0 lolo POpopo";
function stripos2($string,$word){
$retval   = false;
$word_len = strlen($word);
for($i=0;$i<=strlen($string);$i++){
if (strtolower(substr($string,$i,$word_len)) == strtolower($word)){
$retval[0] = true;
$retval[1] = $i;
$retval[2] = $word_len;
}
}
return $retval;
}
function striReplace( $string, $find, $new ){
$r  = false;
while( $p = stripos2( $string, $find ) ){
if( $p[0] ){
$r  = substr( $string, 0, $p[1] );
$r .= $new;
$r .= substr( $string, $p[1] + $p[2] );
}
$string = $r;
}
return $r;
}
echo 'original:<br />' . $string . '<hr>';
echo striReplace( $string, $find, $new );
?>


will

re: Fatidik's function
[!!EDITOR'S NOTE - referenced note removed!!]
No need to write a new function when the following gives the same result:
str_replace(array("\r", "\n"), "", $string)


tom

Note that the $replace parameter is only evaluated once, so if you have something like:
<?php
$item = 1;
$list = str_replace('<li>', $item++.'. ', $list);
?>
then every "li" tag in the input will be replaced with "1. ".


kole

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.


shane43

Keep in mind that if you are trying to remove all the newlines (\n) from a fields that was submitted in a form, you may need look for \r\n instead:
If this doesn't work:
$text=str_replace ("\n", " ", $text);
then try this:
$text=str_replace ("\r\n", " ", $text);


davidwhthomas

Just an update to Dimitri's code:
Replace Once:
I found this one worked best for me:
function replaceOnce($search, $replace, $content){
$pos = strpos($content, $search);
if ($pos === false) { return $content; }
else { return substr($content, 0, $pos) . $replace . substr($content, $pos+strlen($search)); }
}
cheers,
DT


admc

Inspired by the first official example here, that gives "<body text='black'>", I wrote a little function that replaces strings using a "dictionary":
function sablongep($sablon, $helyettesites) {
foreach ($helyettesites as $nev => $adat)
$sablon=str_replace("%$nev%", $adat, $sablon);

return $sablon;
}
Example:
sablongep("We have a lot of fruits: %fruits%, and some vegetables: %vegetables%.",
array( "fruits" => "apple, banana, cherry",
"vegetables" => "carrot, cabbage"));
I admit that this example is not as funny as the others in the manual...


hayley watson

In response to php at azulmx dot com's comment (03-10-2007):
"If your replace string results from an expression or a function, str_replace will evaluate/call it even if the search string is not found."
This is true for ANY function, not just str_replace: the argument expressions are evaluated, and the results are then passed to the function being called.
<?php
$result = str_replace ("zzz", am_i_called (), "this string does not contain the search string");
?>
Is functionally identical to
<?php
$search = "zzz";
$replace = am_i_called();
$string = "this string does not contain the search string";
$result = str_replace($search, $replace, $string);
?>
(except of course for the use of three additional variables!)
This is the main reason why the comma operator is shown as having such low precedence - everything around them has to be evaluated first. And with the parentheses around the argument list, those arguments are evaluated first before the expression that contains the parenthesised expression (i.e., the function call) can be evaluated.


mjaque

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);
?>


aaron

In case anyone doesn't know, Erik's code will not work.  Besides the major syntax flaws, the count parameter returns how many replacements were made, it DOES NOT limit how many are made.  If you really need to replace every other occurrence, this DOES work:
<?php
function str_replace_every_other($needle, $replace, $haystack, &$count=null, $replace_first=true) {
$count = 0;
$offset = strpos($haystack, $needle);
//If we don't replace the first, go ahead and skip it
if (!$replace_first) {
$offset += strlen($needle);
$offset = strpos($haystack, $needle, $offset);
}
while ($offset !== false) {
$haystack = substr_replace($haystack, $replace, $offset, strlen($needle));
$count++;
$offset += strlen($replace);
$offset = strpos($haystack, $needle, $offset);
if ($offset !== false) {
$offset += strlen($needle);
$offset = strpos($haystack, $needle, $offset);
}
}
return $haystack;
}
//Use it like this:
$str = "one two one two one two";
echo str_replace_every_other('one', 'two', $str, $count).'<br />';
//two two one two two two
echo str_replace_every_other('one', 'two', $str, $count, false).'<br />';
//one two two two one two
?>
I added the last option ($replace_first) to let you replace the odd occurrences (true, default) or the evens (false).


canudos01

In BRAZIL we use comma "," insted of dot "." to decimals numbers i use this to convert a user input.
<?php
$valor1 = $_POST["valor1"];
$vir = ',';
$pon = '.';
$v1 = str_replace($vir,$pon, $valor1);
$valor2 = $_POST["valor2"];
$vir = ',';
$pon = '.';
$v2 = str_replace($vir,$pon, $valor2);
function soma($v1,$v2)
{
  return  $v1+$v2  ;
}
echo  soma ($v1,$v2) ;
?>
I hope this help someone


php

If your replace string results from an expression or a function, str_replace will evaluate/call it even if the search string is not found.
function am_i_called ()
{
 echo "oh no, i am
";
 return "";
}
str_replace ("zzz", am_i_called (), "this string does not contain the search string");
str_replace ("zzz", $result = "rats!", "this string does not contain the search string");
echo "$result
";
// oh no, i am
// rats!
also it isn't re-evaluated for multiple replacements...
echo str_replace ("zzz", $result = "r".$result, "zzz zzz zzz zzz zzz zzz zzz");
// rrats! rrats! rrats! rrats! rrats! rrats! rrats!


aidan

If you would like to convert tabs to spaces, you could use the code:
<?php
str_replace("\t", "    ", $text);
?>
However, sometimes this is not good enough. To be technically correct, a tab is not 4 spaces, a tab shifts the text to the next predefined column. Usually these predefined columns occur every 4 spaces from the start of the line.
If you want to convert tabs to spaces while ensuring consistant formatting, try this function...
http://aidanlister.com/repos/v/function.tab2space.php


tapken

If you want to replace only the first occurence of a string you can use this function:
<?php
function str_replace_once($needle, $replace, $haystack) {
   // Looks for the first occurence of $needle in $haystack
   // and replaces it with $replace.
   $pos = strpos($haystack, $needle);
   if ($pos === false) {
       // Nothing found
       return $haystack;
   }
   return substr_replace($haystack, $replace, $pos, strlen($needle));
}
?>


dpardo

If you are working with spanish characters and you are using accent (marks,quotes) you have to use something similar to this
$q = str_replace("&aacute;", "a", $q);


xavier paz xpaz

If both the search and replace params are arrays, str_replace() will apply the substitutions incrementally, it is, it will try the first substitution, then the second using the result from the first one, and so on. It may be OK, or it may be a problem if you only want to change the original text.
For example, consider this code:
<?php
$search = array("one", "two", "three");
$replace = array("two", "three", "one");
$subject = "one two three";
echo str_replace($search, $replace, $subject). "
";
// echoes "one one one"
?>
This function makes the substitutions only to the original text.
<?php
/**
* same as str_replace (array, array, string), but changing only the text in the
* original string
* $search and $replace are arrays of strings, $subject is a string
*/
function str_replace_clean($search, $replace, $subject) {
   if (!is_array($search) or !is_array($replace) or !is_string($subject)) return $subject;
   while (count($search)) {
       // get current terms, reduce the arrays for the next iteration
       $search_text = array_shift($search);
       $replace_text = array_shift($replace);
       // check if the substring is present
       $pos = strpos($subject, $search_text);
       if (is_int($pos)) {
           // match found - break in pieces
           $pieces = explode($search_text, $subject);
           if (count($search)) { // only if there are more substitutions to do
               // make next substitutions in every piece of text between matches
               foreach ($pieces as $k => $v) {
                   if (strlen($v)) $pieces[$k] = str_replace_clean($search, $replace, $v);
               }
           }
           $subject = join($replace_text, $pieces);
           break;
       }
   }
   return $subject;
}
?>
To test:
<?php
echo str_replace_clean($search, $replace, $subject);
// echoes "two three one"
?>
-- Xavier


marc

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.


a_baboshin

I wrote 2 function's file_replace() and dir_replace() with str_replace() signature
function file_replace ($search, $replace, $filename) {
if (file_exists($filename)) {
$cnt = file_get_contents($filename);
if (strstr($cnt, $search)) {
$cnt = str_replace($search, $replace, $cnt);
return file_put_contents($filename, $cnt);
}
return true;
}
return false;
}

function dir_replace ($search, $replace, $dirname, $recursive = true) {
$dir = opendir($dirname);
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
if (is_dir($dirname.'/'.$file)) {
if ($recursive) {
dir_replace($search, $replace, $dirname.'/'.$file);
}
} else {
file_replace($search, $replace, $dirname.'/'.$file);
}
}
}
}
usage ('search', 'replace', '/usr/home/root');


rit

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!


japxrf

I thought this might be handy for someone.
I'm working with a CSV file and noticed that excel tends to put extra quotes around portions of lines that are misformatted. I needed a way to get the section of a line that is surrounded by quotes, remove the quotes and the commas inside those quotes, then put it back into the line.
Don't knock the code - I'm a novice :)
//assuming here that your current line from CSV file is $data2
if(stristr($data2, '"')){ //if there's a quote in this line it needs fixin'
           $quotepos = strpos($data2, '"');
$quotepos_end = strrpos($data2, '"');
$str_len = $quotepos_end - $quotepos;
$badquote = substr($data2, $quotepos, $str_len+1);
$badquote2 = str_replace('"',"",$badquote);
$badquote3 = str_replace(",","",$badquote2);
$data2 = str_replace($badquote, $badquote3, $data2);
}


paul

I made this to parse values returned in a form, but to preserve formatting so that it doesn't get removed in the "remove anything but alphanumeric" line...  Probably not elegant, but it works.
<?php
foreach ($_POST as $key => $val)
 {
   $val = preg_replace("(\r\n|\n|\r)", "#", $val);
   $val = preg_replace("/[^0-9a-z -#]/i",'', $val); // strip anything we don't want
   $val = str_replace("#", "*", $val); // * Put a p or br here.
   $_POST[$key] = $val;
 }
?>


eric

i made this function for http://www.linksback.org it replaces every other occurrence
<?php
function str_replace_every_other($needle, $replace, $haystack) {
   $ii=0
while ( str_replace("$needle", "$replace", "$haystack", 1) {
 while ( str_replace("$needle", "tttttttttt", "$haystack", 1) {  
      $ii++;
 }
}
str_replace("tttttttttt", "$needle","$haystack");  
}
?>


heavyraptor

I had some problems with the function "remove_accents" by joaquin at metaltoad dot com. The problem was that it changed every encoded char, even encoded chars like &gt; (output was g, because the first char after the & is taken).
I changed it a little bit, it works fine now.
<?php
function replace_accents($str) {
 $str = htmlentities($str);
 $str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
 return html_entity_decode($str);
}
?>
Example:
<?php
$str = "äöü ÄÖÜ àéè ÀÉÈ Text &%*'\\" <>";
$str = replace_accents($str);
echo $str; // prints "aou AOU aee AEE Text &%*'" <>"
?>
Have fun


thewolf

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!


the-master

I get annoyed when double triple and more spaces in a row get in the way of my code as they count as characters in wordwrap and therefore ruin the look of the page as the spaces wont show up to the user but the writing look uneaven as the wordwraping has counted the spaces so this might help someone else having this problem.  Pretty simple but should work.
<?php
while(strpos($string, "  ")) {
$string = str_replace(" ", " ", $string); }
?>
Simple but effective code.


tim

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);
}
?>


boris dot christ

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;
}
?>


thewolf

I also wanted to replace rude words in a pice of text, I had  str_replace replacing the word with *** but then I had a thought, what if I could replace these bad words and words also very similar? In the end I wrote this code, it uses two functions, one to check if the similarity between two words is high enough to qualify for filtering out and one to do the actual replacing (modified from my word_replace function):
<?php
$text = 'This is some text with a baword in it.';
echo similar_str_replace('badword', '*******', $text, 80);

function is_similar($one, $two, $similarity) {
   similar_text($one, $two, $percent);
   return $percent >= $similarity ? true : false;
}
/**
* 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
* $similarity(int), how similar the two words must be
*/
function similar_str_replace($search, $replace, $subject, $similarity = 85) {
   return preg_replace('/[a-zA-Z]+/e', 'is_similar(\'\0\', \'' . $search . '\', \'' . $similarity . '\') ? \'' . $replace . '\': \'\0\';', $subject);
}
?>
Just wack that into a php file, its ready for testing!
Hope someone uses it, and perhaps improves it somehow.


mv@anywhere dot br

how to remove accents from text
<?php
function accents($text) {
global $export;
$search  = array ('ç', 'á', 'é', 'í', 'ó', 'ú', 'ã', 'õ', 'â', 'ê', 'î', 'ô', 'û');
$replace = array ('c', 'a', 'e', 'i', 'o', 'u', 'a', 'o', 'a', 'e', 'i', 'o', 'u');
$export    = str_replace($search, $replace, $text);
return $export;
}
accents("Carnaval é só no Brasil");
?>


gwkeeper

Hi, if You want to use case insensitive replacement with support eastern languages e.g. czech special chars try this:
<?php
$text = eregi_replace ( (sql_regcase($searched_text)), "<span class=\"Marked_1\" >\\0</span>", $text );
?>
without sql_regcase it did not found some special eastern chars


darren gates - http://www.tufat.com

Here's a version of str_replace which ignores differences in spacing (like tab, carriage return, line feed, etc.) in the two input strings.
Courtesy of http://www.tufat.com (Darren's Script Archive).
<?php
function str_rep($srcfor,$chwith,$mystr)
{
if(strlen($mystr) == 0)
return "";
$tok  = strtok($srcfor," \n\t\r");
$strsrc = array();
$idxsrc = array();
$i = 0;
while($tok)
{
$strsrc[$i] = $tok;
$pos = strpos($srcfor,$tok);
$idxsrc[$i]=$pos;
$tok = strtok(" \n\r\t");
$i++;
}
$tok2  = strtok($mystr," \n\t\r");
$str = array();
$idx = array();
$j = 0;
while($tok2)
{
$str[$j] = $tok2;
$pos = strpos($mystr,$tok2);
$idx[$j]=$pos;
$tok2 = strtok(" \n\r\t");
$j++;
}
for($m=0;$m<$j;$m++)
{
if(strcasecmp($strsrc[0] , $str[$m]) == 0)
{
for($l=1;$l<$i;$l++)
{
if(strcasecmp($strsrc[$l],$str[$m+$l]) != 0)
break;
}
$l--;
if(($l+1) == $i)
{
$new_str=substr($mystr,0,$idx[$m]);
$new_str .= $chwith;
$index = $idx[$m+$l]+strlen($str[$l+$m]);
$len = strlen($mystr)-$index;
if($len > 0)
$new_str .= str_rep($srcfor,$chwith,substr($mystr,$index,$len));
return $new_str;
break;
}
}
}
return $mystr;
}
?>


xinobit

Here's a function that will replace many times as the last argument ask.
<?
function str_replace_count($search,$replace,$subject,$times) {
$subject_original=$subject;

$len=strlen($search);
$pos=0;
for ($i=1;$i<=$times;$i++) {
$pos=strpos($subject,$search,$pos);

if($pos!==false) {
$subject=substr($subject_original,0,$pos);
$subject.=$replace;
$subject.=substr($subject_original,$pos+$len);
$subject_original=$subject;
} else {
break;
}
}

return($subject);
}
$w="abracadabra";
print str_replace_count("abra","----",$w,2);
?>


03-apr-2006 03:57

Here's a function I put together from pieces other folks wrote. First, it replaces all MS Word smart quotes, EM dashes and EN dashes with their ASCII equivalents, then it goes through and deletes any non-ASCII characters (such as the 1/4 character).  The function was written with backwards-compatibility in mind, not performance. It worked successfully in PHP Version 4.2.3, but not in Version 4.2.2. Hopefully it'll help out someone:
function all_ascii( $stringIn ){
$final = '';
   $search = array(chr(145),chr(146),chr(147),chr(148),chr(150),chr(151));
   $replace = array("'","'",'"','"','-','-');
$hold = str_replace($search[0],$replace[0],$stringIn);
$hold = str_replace($search[1],$replace[1],$hold);
$hold = str_replace($search[2],$replace[2],$hold);
$hold = str_replace($search[3],$replace[3],$hold);
$hold = str_replace($search[4],$replace[4],$hold);
$hold = str_replace($search[5],$replace[5],$hold);
if(!function_exists('str_split')){
  function str_split($string,$split_length=1){
  $count = strlen($string);
  if($split_length < 1){
  return false;
  } elseif($split_length > $count){
  return array($string);
  } else {
  $num = (int)ceil($count/$split_length);
  $ret = array();
  for($i=0;$i<$num;$i++){
  $ret[] = substr($string,$i*$split_length,$split_length);
  }
  return $ret;
  }
  }
}
$holdarr = str_split($hold);
foreach ($holdarr as $val) {
  if (ord($val) < 128) $final .= $val;
}
return $final;
}
Example usage:  echo all_ascii( $myString );


cm

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;
}


aidan

Here is a simple function for highlighting a needle in text. It is careful to not replace text in HTML tags (unless specified).
http://aidanlister.com/repos/v/function.str_highlight.php
Example: The google method of highlighting
<?php
$text = "Here is some text with a search term";
$needle = "search term";
echo str_highlight($text, $needle);
?>
Output:
Here is some text with a <strong>search term</strong>


dbergeron

here is a function to highlight a part of a string. Unlike str_replace, it is both case insensitive, and maintains the case of the highlighted text.
<?php
function highlight($x,$var) {//$x is the string, $var is the text to be highlighted
if ($var != "") {
$xtemp = "";
$i=0;
while($i<strlen($x)){
if((($i + strlen($var)) <= strlen($x)) && (strcasecmp($var, substr($x, $i, strlen($var))) == 0)) {
//this version bolds the text. you can replace the html tags with whatever you like.
$xtemp .= "<b>" . substr($x, $i , strlen($var)) . "</b>";
$i += strlen($var);
}
else {
$xtemp .= $x{$i};
$i++;
}
}
$x = $xtemp;
}
return $x;
}
?>
Example:
<?php
$string = "AaBbCcDd";
$string = highlight($string, "bc");
echo $string; //AaB<b>bC</b>cDd
?>


13-jun-2003 11: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.


mhabr

function str_ireplace (case sensitive) fro PHP 4 and lower
function str_ireplace($search,$replace,$subject) {
$search = preg_quote($search, "/");
return preg_replace("/".$search."/i", $replace, $subject); }


molokoloco

function clean($string) {
$bad = array('\’', '&rsquo;', '…', '&hellip;');
$good = array('\'', '\'', '...', '...');
$string = str_replace($bad, $good, $string);
}
// The result seem to have a bug...
print_r(clean('totot@toto.com'));
//=> totot@toto…com


unusedacct

For those who haven't yet updated to PHP 5 yet, here is a quick function to do regular str_replace on a string [not arrays] only a certain number of times:
<?php
function str_replace_count($find,$replace,$subject,$count)
{
  $subjectnew = $subject;
  $pos = strpos($subject,$find);
  if ($pos !== FALSE)
  {
     while ($pos !== FALSE)
     {
        $nC = $nC + 1;
        $temp = substr($subjectnew,$pos+strlen($find));
        $subjectnew = substr($subjectnew,0,$pos) . $replace . $temp;
        if ($nC >= $count)
        {
           break;
        }
        $pos = strpos($subjectnew,$find);
     } // closes the while loop
  } // closes the if
  return $subjectnew;
}
$stuff = "a b a b a b a b a";
print $stuff . " -- the old string
\n";
print str_replace_count("a ","c ",$stuff,4) . " -- the new string
\n";
// will output c b c b c b c b a -- the new string
?>
Hope this helps.


15-jan-2007 09: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.


yakasha

As the documentation states, remember that arrays are processed in order, take care to not replace your replacements:
<?php
$s = 'apple\'s';
$f = array('\'', '\\');
$r = array('\\\'', '\\\\');
echo str_replace($f, $r, $s);  // Produces:  apple\\'s
$f = array('\\', '\'');
$r = array('\\\\', '\\\'');
echo str_replace($f, $r, $s); // Produces:  apple\'s
?>


matt wheaton

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);
?>


imho

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.


ahmad

an enhanced version of Dmitry Fedotov's function that can handle arrays as input and output arguments ...
<?
$string = "test1 test2 test3 test4";
echo str_replace_once('test', 'testing', $string);
// testing1 test2 test3 test4
$string = ".NET sucks, Java sucks, PHP sucks"; // that doesn't sound right ... lets fix it!
echo str_replace_once("sucks", array("is stupid", "is ugly", "rules"), $string);
// .NET is is stupid, Java is ugly, PHP rules
function str_replace_once($search, $replace, $subject, &$offset = 0) {
if (is_array($search)) {
if (is_array($replace)) {
foreach ($search as $x => $value) $subject = str_replace_once($value, $replace[$x], $subject, $offset);
} else {
foreach ($search as $value) $subject = str_replace_once($value, $replace, $subject, $offset);
}
} else {
if (is_array($replace)) {
foreach ($replace as $value) $subject = str_replace_once($search, $value, $subject, $offset);
} else {
$pos = strpos($subject, $search, $offset);
if ($pos !== false) {
$offset = $pos+strlen($search);
$subject = substr($subject, 0, $pos) . $replace . substr($subject, $offset);
}
}
}

return $subject;
}
?>


rylas

An easy way to convert email addresses to the PHP.net comment-style email addresses:
<?
$email = "john@doe.org";
$search = array('@', '.');
$replace = array(" at ", " dot ");
$result = str_replace($search, $replace, $email);
?>
Outputs:
john at doe dot org


chirag

All the Google-like highlight functions above mess up when the needle is within the url too. getHTMLHighlight function below works fine:
<?php
function getHTMLHighlight($needle, $haystack, $hlS, $hlE)
{
  $parts = explode(">", $haystack);
   foreach($parts as $key=>$part)
  {
    $pL = "";
      $pR = "";
    if(($pos = strpos($part, "<")) === false)
      $pL = $part;
    elseif($pos > 0)
    {
      $pL = substr($part, 0, $pos);
      $pR = substr($part, $pos, strlen($part));
    }
    if($pL != "")
      $parts[$key] = preg_replace('|\b('.quotemeta($needle).')\b|iU', $hlS.'\\1'.$hlE, $pL) . $pR;
  }
  return(implode(">", $parts));
}
?>
Usage:
getHTMLHighlight($needle, $haystack, "<b style=\"background-color:#FF3145\">", "</b>");


tlg

About xinobit's function -> what if $search, $replace or/and $subject are arrays?.. ;)
I supplemented the code to support that possibility but the result was a bit heavy... and slow!
preg_replace() is about 5 times faster! :) (more or less depending on the length of $subject)
So, a little example of function:
<?php
function str_replace_limit($search, $replace, $subject, $limit=-1) {
   // constructing mask(s)...
   if (is_array($search)) {
       foreach ($search as $k=>$v) {
           $search[$k] = '`' . preg_quote($search[$k],'`') . '`';
       }
   }
   else {
       $search = '`' . preg_quote($search,'`') . '`';
   }
   // replacement
   return preg_replace($search, $replace, $subject, $limit);
}
?>
(of course you could insert a "&$count" argument for PHP >= 5.1.0)


hermes

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!


quentin dot t

A little enhancement to heavyraptor's replace_accents (simply adding 'cedil' and 'ring' character variations) :
<?php
function replace_accents($s) {
$s = htmlentities($s);
$s = preg_replace ('/&([a-zA-Z])(uml|acute|grave|circ|tilde|cedil|ring);/', '$1', $s);
$s = html_entity_decode($s);
return $s;
}
?>
Cheers.


tjomi4

<?php
function utf8_str_replace($s,$r,$str){
# utf8 str_replace
# www.yeap.lv
if(!is_array($s)){
 $s = '!'.preg_quote($s,'!').'!u';
 }else{
 foreach ($s as $k => $v) {
  $s[$k] = '!'.preg_quote($v).'!u';
 }
}
return preg_replace($s,$r,$str);
}
?>


howard yeend: http://www.puremango.co.uk

<?php
/*
a function to replace *all* occurrences of $search
(aka recursive str_replace)
echo str_replace("--","-","a-b--c---d------e");
->returns [a-b-c--d---e]
echo str_replace_all("--","-","a-b--c---d------e")."<br />";
->return [a-b-c-d-e]
*/
function str_replace_all($search,$replace,$subject) {
 while(strpos($subject,$search)!==false) {
$subject = str_replace($search,$replace,$subject);
 }
 return $subject;
}
echo str_replace("--","-","a-b--c---d------e")."<br />";
echo str_replace_all("--","-","a-b--c---d------e")."<br />";
?>


admin

<?
# This is a complete profanity filter. I couldn't get any of the preg_replace statements working, so I made one that is CASE INSENSITIVE, SPACE INSENSITIVE too.
$original="This contains curseword, curse_word1, cursewor_d2, cursewo_rd3, and a bunch of other text...";
echo "Before: $original
";
$original=RemoveCurseWords($original);
echo "After: $original
";
function RemoveCurseWords($original) {
$patterns[0] = 'curseword';
$patterns[1] = 'curse_word1';
$patterns[2] = 'cursewor_d2';
$patterns[3] = 'cursewo_rd3';
$finalremove=$original;
$piece_front="";
$piece_back="";
$piece_replace="***";
for ($x=0; $x < count($patterns); $x++) {
$safety=0;
# Prevents infinity loops if you accidentally put in replacements that match your patterns. it could happen! :)
# The saftey could cause issues if you are replacing more than 100,000 occurrences in a string, so remove it if you trust it.
while(strstr(strtolower($finalremove),strtolower($patterns[$x]))) {
# find & remove all occurrence

$safety=$safety+1;
if ($safety >= 100000) { break; }
$occ=strpos(strtolower($finalremove),strtolower($patterns[$x]));
$piece_front=substr($finalremove,0,$occ);
$piece_back=substr($finalremove,($occ+strlen($patterns[$x])));
$finalremove=$piece_front . $piece_replace . $piece_back;
} # while

} # for
return $finalremove;
}# function RemoveCurseWords


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker