windowshater try to change/switch the script file encoding to ANSI/UTF-8. I had some problems with that and I solved them this way.
str_replace
(PHP 4, PHP 5)
str_replace — 발견한 모든 검색 문자열을 치환 문자열로 교체
설명
subject에서 발견한 모든 search를 주어진 replace 값으로 치환한 문자열이나 배열을 반환합니다.
(정규표현식처럼) 복잡한 치환 규칙이 필요하지 않다면, ereg_replace()나 preg_replace() 대신 이 함수를 사용해야 합니다.
인수
search와 replace가 배열이면, str_replace()는 각가의 배열에서 취한 값을 사용해서 subject를 검색 치환합니다. replace가 search보다 적은 값을 가지고 있으면, 나머지 치환값으로 빈 문자열을 사용합니다. search가 배열이고 replace가 문자열이면, search의 모든 값에 이 치환 문자열을 사용합니다. 반대도 마찬가지입니다.
search나 replace가 배열이면, 그 요소는 처음부터 마지막 순으로 처리됩니다.
- search
-
- replace
-
- subject
-
subject가 배열이면, 검색 및 치환을 subject의 모든 원소에서 수행하고, 배열을 반환합니다.
- count
- 넘겨지면, 매치하여 교체하는 수를 가집니다.
반환값
이 함수는 치환한 값을 가지는 문자열이나 배열을 반환합니다.
변경점
| 버전 | 설명 |
|---|---|
| 5.0.0 | count 인수 추가. |
| 4.3.3 | 이 함수의 동작이 바뀌었습니다. 이전 버전에서는 search와 replace 인수를 배열로 사용할 때, search 인덱스가 비어 있을 경우 replace 배열의 내부 포인트를 증가하지 않고 지나치는 버그가 있었습니다. 이는 PHP 4.3.3에서 수정되었으므로, 이 버그에 의존하고 있던 모든 스크립트는 이전의 이상한 동작을 재현하려면 호출 전에 빈 search 값을 제거해야 합니다. |
| 4.0.5 | 대부분의 인수가 array를 받습니다. |
예제
Example #1 str_replace() 예제
<?php
// 결과: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// 결과: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// 결과: 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);
// PHP 5.0.0부터 사용할 수있는 count 인수 사용
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
// 교체 순서
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order = array("\r\n", "\n", "\r");
$replace = '<br />';
// \r\n을 먼처 처리해서 두번 변환되지 않도록 합니다.
$newstr = str_replace($order, $replace, $str);
// 출력: apearpearle pear
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>
참고
- str_ireplace() - 대소문자 구분 없는 str_replace
- substr_replace() - Replace text within a portion of a string
- preg_replace() - 정규 표현식 검색과 치환을 수행
- strtr() - Translate characters or replace substrings
dmartineztrilla at hotmail dot com
14-Feb-2012 02:17
windowshater
24-Nov-2011 12:15
I hope this helps someone who has gone insane for 4 hours...
If you have someone posting weird windows curly quotes into your forms and you cannot replace them using str_replace.
I tried using mb_convert_encoding to convert the string to UTF-8 and then to do a str_replace but still no joy.
I tried to str_replace using the \xe2\x80\x98 codes and also using the chr(145), chr(146), chr(147) codes.
Nothing worked... It would replace the quotes but then ADD a question mark in a diamond !!!! no idea why..
in the end i just url encoded the string and replaced teh html codes:
$search_weird_chars = array( '%E2%80%99',
'%E2%80%99',
'%E2%80%98',
'%E2%80%9C',
'%E2%80%9D'
);
$this->post_array[$key] = urlencode($this->post_array[$key]);
$this->post_array[$key] = str_replace($search_weird_chars, "'", $this->post_array[$key]);
$this->post_array[$key] = urldecode($this->post_array[$key]);
this works although it is a totally retarded thing to have to do .. oh well. :-)
moostende at gmail dot com
26-Sep-2011 11:20
Note that this does not replace strings that become part of replacement strings. This may be a problem when you want to remove multiple instances of the same repetative pattern, several times in a row.
If you want to remove all dashes but one from the string '-aaa----b-c-----d--e---f' resulting in '-aaa-b-c-d-e-f', you cannot use str_replace. Instead, use preg_replace:
<?php
$challenge = '-aaa----b-c-----d--e---f';
echo str_replace('--', '-', $challenge).'<br>';
echo preg_replace('/--+/', '-', $challenge).'<br>';
?>
This outputs the following:
-aaa--b-c---d-e--f
-aaa-b-c-d-e-f
matthew at euphoriccode dot com
09-Sep-2011 10:52
If you are looking to implement str with password generation the following function will help;
<?php
function gen_pass_process($girth = 17) {
$str = '';
for($1 = 0; $i<$girth; $i++) {
$str . = chr(rand(48,122));
}
return $str;
}
?>
didiergm
29-Jul-2011 03:25
php 5.3.2 (may exist before, but it never saw it until today)
the replace is always evaluated before the search takes place.
So if like me you use str_replace to replace some macros in the html code, and these replacement call a function, you must be aware that the functions always runs, not only when the search is found.
for example:
str_replace (
array ('%macro1%', '%macro2%'),
array (myfunction1(), myfunction2(),
thesource);
myfunction1() and myfunction2() are called regardless of the fact that the source contains %macro1%' or '%macro2%' or not. I genuinely believed that myfunction1 would only be called if %macro1% was found (resp myfunction2 and %macro2%)
I noticed it because myfunction1 was running a database query to increment a value and I noticed that this value was incremented w/o need. It took me a wee while to get to the bottom of it
mbullard at accuvista dot co dot uk
14-Jul-2011 02:42
Insert space after comma.
If you have a form that stores results in a database field as comma separated values, when you display this data you can use the following to insert a space after each comma:
<?php
$find[] = ',';
$replace[] = ', ';
$text = str_replace($find, $replace, $row_rsRecordset['Field']);
print_r($text);
?>
Notes:
1) To get round the Replacement Order Gotcha, the comma is also replaced with its code equivalent: ,
2) You can adapt the $replace section to suit your needs: swap out the code with <br/> or replace comma and space with · etc.
hakre
06-Jul-2011 08:08
I was looking for a str_replace function supporting callbacks. As I didn't found one I wrote one my own. Works exactly like str_replace, but the replace parameter is a callback or an array of callbacks (instead of string/strings in str_replace). The callback function accepts two arguments, the string that is being replaced and the count of the replacement being done.
<?php
/**
* str_ureplace
*
* str_replace like function with callback
*
* @param string|array search
* @param callback|array $replace
* @param string|array $subject
* @param int $replace_count
* @return string|array subject with replaces, FALSE on error.
*/
function str_ureplace($search, $replace, $subject, &$replace_count = null) {
$replace_count = 0;
// validate input
$search = array_values((array) $search);
$searchCount = count($search);
if (!$searchCount) {
return $subject;
}
foreach($search as &$v) {
$v = (string) $v;
}
unset($v);
$replaceSingle = is_callable($replace);
$replace = $replaceSingle ? array($replace) : array_values((array) $replace);
foreach($replace as $index=>$callback) {
if (!is_callable($callback)) {
throw new Exception(sprintf('Unable to use %s (#%d) as a callback', gettype($callback), $index));
}
}
// search and replace
$subjectIsString = is_string($subject);
$subject = (array) $subject;
foreach($subject as &$haystack) {
if (!is_string($haystack)) continue;
foreach($search as $key => $needle) {
if (!$len = strlen($needle))
continue;
$replaceSingle && $key = 0;
$pos = 0;
while(false !== $pos = strpos($haystack, $needle, $pos)) {
$replaceWith = isset($replace[$key]) ? call_user_func($replace[$key], $needle, ++$replace_count) : '';
$haystack = substr_replace($haystack, $replaceWith, $pos, $len);
}
}
}
unset($haystack);
return $subjectIsString ? reset($subject) : $subject;
}
?>
ahmed dot 3abdolah at gmail dot com
24-Jun-2011 11:05
Hello , this is a simple function programed by str_replace for crypting password and ....etc .
You can do verry much think with str_replace :
look at this:
<?PHP
/*
*This Function Is programed By MR-O
*The first arabic Function for crypting
*/
function abo($variable){
$variable =str_replace("1",1 ^ 52 ,$variable);
$variable =str_replace("2",2 ^ 5 ,$variable);
$variable =str_replace("3",3 ^ 8 ,$variable);
$variable =str_replace("4",4 ^ 18 ,$variable);
$variable =str_replace("5",5 ^ 30 ,$variable);
$variable =str_replace("6",6 ^ 42 ,$variable);
$variable =str_replace("7",7 ^ 26 ,$variable);
$variable =str_replace("8",8 ^ 62 ,$variable);
$variable =str_replace("9",9 ^ 1 ,$variable);
$variable =str_replace("0",0 ^ 3 ,$variable);
$variable =str_replace("a","m8" ,$variable);
$variable =str_replace("b","2s" ,$variable);
$variable =str_replace("c","9e" ,$variable);
$variable =str_replace("d","6s" ,$variable);
$variable =str_replace("e","a3" ,$variable);
$variable =str_replace("f","m3" ,$variable);
$variable =str_replace("g","6f" ,$variable);
$variable =str_replace("h","9c" ,$variable);
$variable =str_replace("i","85" ,$variable);
$variable =str_replace("j","32" ,$variable);
$variable =str_replace("k","1a" ,$variable);
$variable =str_replace("l","6e" ,$variable);
$variable =str_replace("m","5f" ,$variable);
$variable =str_replace("n","9r" ,$variable);
$variable =str_replace("o","z0" ,$variable);
$variable =str_replace("p","y6" ,$variable);
$variable =str_replace("q","9i" ,$variable);
$variable =str_replace("r","y9" ,$variable);
$variable =str_replace("s","5g" ,$variable);
$variable =str_replace("t","p3" ,$variable);
$variable =str_replace("u","d5" ,$variable);
$variable =str_replace("v","t8" ,$variable);
$variable =str_replace("w","2x" ,$variable);
$variable =str_replace("x","6w" ,$variable);
$variable =str_replace("y","v9" ,$variable);
$variable =str_replace("z","zv" ,$variable);
$variable =str_replace("A","m8" ,$variable);
$variable =str_replace("B","24d" ,$variable);
$variable =str_replace("C","9e" ,$variable);
$variable =str_replace("D","6s2" ,$variable);
$variable =str_replace("E","qw" ,$variable);
$variable =str_replace("F","oe" ,$variable);
$variable =str_replace("G","0t" ,$variable);
$variable =str_replace("H","o9" ,$variable);
$variable =str_replace("I","74" ,$variable);
$variable =str_replace("J","dd" ,$variable);
$variable =str_replace("K","2b" ,$variable);
$variable =str_replace("L","rt" ,$variable);
$variable =str_replace("M","0c" ,$variable);
$variable =str_replace("N","2w" ,$variable);
$variable =str_replace("O","te" ,$variable);
$variable =str_replace("P","sc" ,$variable);
$variable =str_replace("Q","uy" ,$variable);
$variable =str_replace("R","hp" ,$variable);
$variable =str_replace("S","h1" ,$variable);
$variable =str_replace("T","s5" ,$variable);
$variable =str_replace("U","fs" ,$variable);
$variable =str_replace("V","e1s" ,$variable);
$variable =str_replace("W","1y" ,$variable);
$variable =str_replace("X","dv" ,$variable);
$variable =str_replace("Y","w2" ,$variable);
$variable =str_replace("Z","w5" ,$variable);
$variable = str_replace('1','5b',$variable);
$variable = str_replace('2','b1',$variable);
$variable = str_replace('3','0j',$variable);
$variable = str_replace('4','1e',$variable);
$variable = str_replace('5','z2',$variable);
$variable = str_replace('6','15e',$variable);
$variable = str_replace('7','d2',$variable);
$variable = str_replace('8','e5b',$variable);
$variable = str_replace('9','y8',$variable);
$variable = str_replace('0','g1y7',$variable);
echo $variable ;
}
?>
thanks , MR-O .
biohazard dot ge at gmail dot com
20-May-2011 02:33
Replace and match strings between two needles in large haystack quicker (?):
<?php
//Assume you have input:
$input = "<p> Images
<img src='some/image_1.jpg'> This is a cat
and <img src=some/image_2.jpg> That is a bat
and <img src="some/image_3.jpg"> What is this
and <img src=some/image_4.jpg> What is that
</p>
";
between_replace ('<img src="','">', $input, "{*}");
between_replace ('<img src=\'','\'>', $input, "{*}");
between_replace ('<img src=','>', $input, "'{*}'");
//As result we have
//$input == "<p> Images
// <img src='some/image_1.jpg'> This is a cat
//and <img src='some/image_2.jpg'> That is a bat
//and <img src='some/image_3.jpg'> What is this
//and <img src='some/image_4.jpg'> What is that
//</p>
//";
$result = between_fetch ("<img src='","'>", $input);
// $result == array ('some/image_1.jpg', 'some/image_2.jpg', 'some/image_3.jpg', 'some/image_4.jpg');
?>
Not a good example but only with them and str_replace I parsed word converted html for epub. This is what people invent not willing to get familiar with regular expressions...
<?php
function between_replace ($open, $close, &$in, $with, $limit=false, $from=0)
{
if ($limit!==false && $limit==0)
{
return $in;
}
$open_position = strpos ($in, $open, $from);
if ($open_position===false)
{
return false;
};
$close_position = strpos ($in, $close, $open_position+strlen($open));
if ($close_position===false)
{
return false;
};
$current = false;
if (strpos($with,'{*}')!==false)
{
$current = substr ($in, $open_position+strlen($open), $close_position-$open_position-strlen($open));
$current = str_replace ('{*}',$current,$with);
//debug_echo ($current);
}
else
{
$current = $with;
}
$in = substr_replace ($in, $current, $open_position+strlen($open), $close_position-$open_position-strlen($open));
$next_position = $open_position + strlen($current) + 1;
if ($next_position>=strlen($in))
{
return false;
}
if ($limit!==false)
{
$limit--;
}
between_replace ($open, $close, $in, $with, $limit, $next_position);
return $in;
}
function between_fetch ($open, $close, &$in, &$result=null, $with='', $limit=false, $from=0)
{
if ($limit!==false && $limit==0)
{
return $in;
};
if (!is_array($result))
{
$result = array ();
}
$open_position = strpos ($in, $open, $from);
if ($open_position===false)
{
return false;
};
$close_position = strpos ($in, $close, $open_position+strlen($open));
if ($close_position===false)
{
return false;
};
$current = substr ($in, $open_position+strlen($open), $close_position-$open_position-strlen($open));
$next_position = $open_position + strlen($current) + 1;
if ($with!='')
{
$current = str_replace('{*}', $current, $with);
}
$result[] = $current;
if ($next_position>=strlen($in))
{
return false;
}
if ($limit!==false)
{
$limit--;
}
between_fetch ($open, $close, $in, $result, $with, $limit, $next_position);
return $in;
}
?>
jay_knows_(all)uk at hotmail dot com
16-Feb-2011 07:16
This strips out horrible MS word characters.
Just keep fine tuning it until you get what you need, you'll see ive commented some out which caused problems for me.
There could be some that need adding in, but its a start to anyone who wishes to make their own custom function.
<?php
function msword_conversion($str)
{
$str = str_replace(chr(130), ',', $str); // baseline single quote
$str = str_replace(chr(131), 'NLG', $str); // florin
$str = str_replace(chr(132), '"', $str); // baseline double quote
$str = str_replace(chr(133), '...', $str); // ellipsis
$str = str_replace(chr(134), '**', $str); // dagger (a second footnote)
$str = str_replace(chr(135), '***', $str); // double dagger (a third footnote)
$str = str_replace(chr(136), '^', $str); // circumflex accent
$str = str_replace(chr(137), 'o/oo', $str); // permile
$str = str_replace(chr(138), 'Sh', $str); // S Hacek
$str = str_replace(chr(139), '<', $str); // left single guillemet
// $str = str_replace(chr(140), 'OE', $str); // OE ligature
$str = str_replace(chr(145), "'", $str); // left single quote
$str = str_replace(chr(146), "'", $str); // right single quote
// $str = str_replace(chr(147), '"', $str); // left double quote
// $str = str_replace(chr(148), '"', $str); // right double quote
$str = str_replace(chr(149), '-', $str); // bullet
$str = str_replace(chr(150), '-–', $str); // endash
$str = str_replace(chr(151), '--', $str); // emdash
// $str = str_replace(chr(152), '~', $str); // tilde accent
// $str = str_replace(chr(153), '(TM)', $str); // trademark ligature
$str = str_replace(chr(154), 'sh', $str); // s Hacek
$str = str_replace(chr(155), '>', $str); // right single guillemet
// $str = str_replace(chr(156), 'oe', $str); // oe ligature
$str = str_replace(chr(159), 'Y', $str); // Y Dieresis
$str = str_replace('°C', '°C', $str); // Celcius is used quite a lot so it makes sense to add this in
$str = str_replace('£', '£', $str);
$str = str_replace("'", "'", $str);
$str = str_replace('"', '"', $str);
$str = str_replace('–', '–', $str);
return $str;
}
?>
Oleg Butuzov butuzov at made dot com dot ua
01-Feb-2011 07:26
Sometimes we need replace just first occurrence , a small function that can help you.
<?php
$string = 'alpharomero alpha arlekino';
var_dump(str_replace_once('alpha','[replaced]', $string));
var_dump($string);
echo '<hr>';
var_dump(str_replace_once('a','[replaced]', $string));
var_dump($string);
echo '<hr>';
var_dump(str_replace_once('abnormal','[replaced]', $string));
var_dump($string);
echo '<hr>';
var_dump(str_replace_once('n','[replaced]', $string));
var_dump($string);
echo '<hr>';
function str_replace_once($str_pattern, $str_replacement, $string){
if (strpos($string, $str_pattern) !== false){
$occurrence = strpos($string, $str_pattern);
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
}
return $string;
}
?>
mevdschee
29-Jan-2011 03:19
say you want every "a" replaced by "abba" and every "b" replaced by "baab" in the string "ab" you do:
<?php
$search = array("a","b");
$replace = array("abba","baab");
echo str_replace($search,$replace,"ab");
?>
that results in "abaabbaababaab" and not the expected "abbabaab"
I wrote this little snippet to solve the replacements-in-replacement problem:
<?php
function search_replace($s,$r,$sql)
{ $e = '/('.implode('|',array_map('preg_quote', $s)).')/';
$r = array_combine($s,$r);
return preg_replace_callback($e, function($v) use ($s,$r) { return $r[$v[1]]; },$sql);
}
echo search_replace($search,$replace,"ab");
?>
that results in the expected "abbabaab"
Viam
17-Jan-2011 09:57
This way it is possible to make a encryption of some sort
A small script i wrote.
Proof of concept:
<?php
//Data to be encrypted.Take note that it has to be text.
$Input_data = "i want a apple";//Place the text here
$Encrypter_variables= array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
$Variables_replace = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "%", "!", "@", "#", "$", "^", "&", "*","~", "+", ":", "-", "=", "?", "'", ".", "<");
$New_data = str_replace($Encrypter_variables, $Variables_replace, $Input_data);
echo $New_data;//Displays the encrypted data,In this case it is 9 ?1$: 1 1&&@5
//Data to be decrypted
$Output_data = "9 ?1$: 1 1&&@5";////Place the encrypted data here
$Decrypter_variables = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "%", "!", "@", "#", "$", "^", "&", "*","~", "+", ":", "-", "=", "?", "'", ".", "<");
$Variables_assign = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
$Newer = str_replace($Decrypter_variables, $Variables_assign , $Output_data);
echo $Newer;//Displays the decrypted data Which is i want a apple
?>
Steve
13-Jan-2011 06:22
Just a note about the json_decode solution from nikolaz dot tang at hotmail dot com
It may return an instance of stdClass, which may not quite be what you want. (using multi-dimensional arrays)
IF you want to be sure, stick to the proper recursive solution.
nikolaz dot tang at hotmail dot com
11-Nov-2010 08:57
A faster way to replace the strings in multidimensional array is to json_encode() it, do the str_replace() and then json_decode() it, like this:
<?php
function str_replace_json($search, $replace, $subject){
return json_decode(str_replace($search, $replace, json_encode($subject)));
}
?>
This method is almost 3x faster (in 10000 runs.) than using recursive calling and looping method, and 10x simpler in coding.
Compared to:
<?php
function str_replace_deep($search, $replace, $subject)
{
if (is_array($subject))
{
foreach($subject as &$oneSubject)
$oneSubject = str_replace_deep($search, $replace, $oneSubject);
unset($oneSubject);
return $subject;
} else {
return str_replace($search, $replace, $subject);
}
}
?>
jpoirier at hotmail.fr
25-Jun-2010 05:40
I've found something interesting about the $count param:
As of 5.0.0 the last parameter (count) is passed by reference (it is optional). So 2 choices :
1)passed not by reference - it's limit
2)passed by reference : the var is is_null() - all occurences are replaced the number is returned in "count", otoh if the var !is_null() -> use as limit.
On version 5.3.0 the 4th parameter, count, does not work when not passed
by reference.
http://bugs.php.net/bug.php?id=11457
Hope this help
apmuthu at usa dot net
19-Jun-2010 12:31
If we have a html template that contains placeholders in curly braces that need to be replaced in runtime, the following function will do it using str_replace:
<?php
function parse_template($filename, $data) {
// example template variables {a} and {bc}
// example $data array
// $data = Array("a" => 'one', "bc" => 'two');
$q = file_get_contents($filename);
foreach ($data as $key => $value) {
$q = str_replace('{'.$key.'}', $value, $q);
}
return $q;
}
?>
cableray
11-Jun-2010 12:03
If you wish to get around the 'gotcha', you could do something like this:
<?php
$find=array('a', 'p', '^', '*');
$replace = array('^', '*', 'apple', 'pear');
str_replace($find, $replace, 'a p');
?>
The idea here is that you first replace the items you want with unique identifiers (that you are unlikely to find in the subject) and then search for those identifiers and then replace them.
christian dot reinecke at web dot de
14-May-2010 12:06
If you need to replace a string in another, but only once but still in all possible combinations (f.e. to replace "a" with "x" in "aba" to get array("xba", "abx")) you can use this function:
<?php
function getSingleReplaceCombinations($replace, $with, $inHaystack)
{
$splits = explode($replace, $inHaystack);
$result = array();
for ($i = 1, $ix = count($splits); $i < $ix; ++$i) {
$previous = array_slice($splits, 0, $i);
$next = array_slice($splits, $i);
$combine = array_pop($previous) . $with . array_shift($next);
$result[] = implode($replace, array_merge($previous, array($combine), $next));
}
return $result;
}
var_dump(getSingleReplaceCombinations("a", "x", "aba")); // result as mentioned above
?>
It may not be the best in performance, but it works.
jbarnett at jmbelite dot com
26-Apr-2010 01:23
Might be worth mentioning that a SIMPLE way to accomplish Example 2 (potential gotchas) is to simply start your "replacements" in reverse.
So instead of starting from "A" and ending with "E":
<?php
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
// replaces A to B, B to C, C to D, D to E, E to F (makes them all F)
// start from "E" and end with "A":
$search = array('E', 'D', 'C', 'B', 'A');
$replace = array('F', 'E', 'D', 'C', 'B');
// replaces E to F, D to E, C to D, B to C, A to B (prevents from
// multiple replacements of already replaced values)
?>
So basically start from the "end" and put the replacements in an order where the "replaced value" won't equal a value that exists later in the "search array".
pjcdawkins at googlemail dot com
23-Apr-2010 05:23
Here's a deep replace function allowing multi-dimensional arrays in $search, $replace and $subject. The keys and other structure of $subject are preserved.
<?php
// Auxiliary function:
function _replaceWithAnything($search,$replace,$subject){
if(!is_array($search) || !is_array($replace)){
$search=array($search);
$replace=array($replace);
}
$match=array_search($subject,$search,true);
if($match!==false && array_key_exists($match,$replace))
$subject=$replace[$match];
return $subject;
}
// Main function:
function deepReplace($search,$replace,$subject){
if(!is_array($subject))
return _replaceWithAnything($search,$replace,$subject);
foreach($subject as &$val){
if(is_array($val)){
$val=deepReplace($search,$replace,$val);
continue;
}
$val=_replaceWithAnything($search,$replace,$val);
}
return $subject;
}
?>
fleshgrinder at gmx dot at
16-Apr-2010 05:50
Fast function to replace new lines from a given string. This is interesting to replace all new lines from e. g. a text formatted in HTML retrieved from database and printing it without the unnecessary new lines. This results in slightly faster rendering in the Web browser.
<?php
/**
* Replace all linebreaks with one whitespace.
*
* @access public
* @param string $string
* The text to be processed.
* @return string
* The given text without any linebreaks.
*/
function replace_newline($string) {
return (string)str_replace(array("\r", "\r\n", "\n"), '', $string);
}
?>
Wes Foster
17-Dec-2009 01:32
Feel free to optimize this using the while/for or anything else, but this is a bit of code that allows you to replace strings found in an associative array.
For example:
<?php
$replace = array(
'dog' => 'cat',
'apple' => 'orange'
'chevy' => 'ford'
);
$string = 'I like to eat an apple with my dog in my chevy';
echo str_replace_assoc($replace,$string);
// Echo: I like to eat an orange with my cat in my ford
?>
Here is the function:
<?php
function strReplaceAssoc(array $replace, $subject) {
return str_replace(array_keys($replace), array_values($replace), $subject);
}
?>
[Jun 1st, 2010 - EDIT BY thiago AT php DOT net: Function has been replaced with an updated version sent by ljelinek AT gmail DOT com]
jonathon at compwright dot com
12-Nov-2009 09:23
If you get a blank page when passing an object to str_replace() (relying on __toString() to convert the object to a string) then you may need to force string context using (string), like so:
<?php
private function _load_vars($vars, &$source = false) {
if(!$source) $source =& $this->code;
foreach((array)$vars as $key => $var) {
$source = str_replace("{".$key."}", (string) $var, $source);
}
}
}
?>
I was running Ubuntu Server with PHP 5.2.6 and getting apache segfaults and mysterious blank pages when $var happened to be certain objects.
Chris
01-Nov-2009 08:22
Compress a string's internal spaces:
<?php
$str = ' This is a test ';
$count = 1;
while($count)
$str = str_replace(' ', ' ', $str, $count);
?>
Decko
15-Oct-2009 01:41
As mentioned earlier you should take the order into account when substituting multiple values.
However it is worth noticing that str_replace doesn't seem to re-read the string when doing single replacements. Take the following example.
<?php
$s = '/a/a/';
$s = str_replace('/a/', '/', $s);
?>
You would expect the following.
First replacement '/a/a/' -> '/a/'
Second replacement '/a/'->'/'
This is not the case, the actual result will be '/a/'.
To fix this, you will have to put str_replace in a while-loop.
<?php
$s = '/a/a/';
while(strpos($s, '/a/') !== false)
$s = str_replace('/a/', '/', $s); //eventually $s will == '/'
?>
Alberto Lepe
15-Jun-2009 07:44
Be careful when replacing characters (or repeated patterns in the FROM and TO arrays):
For example:
<?php
$arrFrom = array("1","2","3","B");
$arrTo = array("A","B","C","D");
$word = "ZBB2";
echo str_replace($arrFrom, $arrTo, $word);
?>
I would expect as result: "ZDDB"
However, this return: "ZDDD"
(Because B = D according to our array)
To make this work, use "strtr" instead:
<?php
$arr = array("1" => "A","2" => "B","3" => "C","B" => "D");
$word = "ZBB2";
echo strtr($word,$arr);
?>
This returns: "ZDDB"
moz667 at gmail dot com
21-May-2009 09:49
<?php
/*
This is a function for made recursive str_replaces in an array
*/
function recursive_array_replace($find, $replace, &$data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
if (is_array($value)) {
recursive_array_replace($find, $replace, $data[$key]);
} else {
$data[$key] = str_replace($find, $replace, $value);
}
}
} else {
$data = str_replace($find, $replace, $data);
}
}
$a = array();
$a['a'] = "a";
$a['b']['a'] = "ba";
$a['b']['b'] = "bb";
$a['c'] = "c";
$a['d']['a'] = "da";
$a['d']['b'] = "db";
$a['d']['c'] = "dc";
$a['d']['d'] = "dd";
echo "Before Replaces";
print_r($a);
recursive_array_replace("a", "XXXX", $a);
echo "After Replaces";
print_r($a);
?>
michael dot moussa at gmail dot com
29-Jan-2009 06:38
As previous commentators mentioned, when $search contains values that occur earlier in $replace, str_replace will factor those previous replacements into the process rather than operating solely on the original string. This may produce unexpected output.
Example:
<?php
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'ABCDE';
echo str_replace($search, $replace, $subject); // output: 'FFFFFF'
?>
In the above code, the $search and $replace should replace each occurrence in the $subject with the next letter in the alphabet. The expected output for this sample is 'BCDEF'; however, the actual output is 'FFFFF'.
To more clearly illustrate this, consider the following example:
<?php
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject); // output: 'F'
?>
Since 'A' is the only letter in the $search array that appears in $subject, one would expect the result to be 'B'; however, replacement number $n does *not* operate on $subject, it operates on $subject after the previous $n-1 replacements have been completed.
The following function utilizes array_combine and strtr to produce the expected output, and I believe it is the most efficient way to perform the desired string replacement without prior replacements affecting the final result.
<?php
/**
* When using str_replace(...), values that did not exist in the original string (but were put there by previous
* replacements) will be replaced continuously. This string replacement function is designed replace the values
* in $search with those in $replace while not factoring in prior replacements. Note that this function will
* always look for the longest possible match first and then work its way down to individual characters.
*
* The "o" in "stro_replace" represents "original", indicating that the function operates only on the original string.
*
* @param array $search list of strings or characters that need to be replaced
* @param array $replace list of strings or characters that will replace the corresponding values in $search
* @param string $subject the string on which this operation is being performed
*
* @return string $subject with all substrings in the $search array replaced by the values in the $replace array
*/
function stro_replace($search, $replace, $subject)
{
return strtr( $subject, array_combine($search, $replace) );
}
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'ABCDE';
echo stro_replace($search, $replace, $subject); // output: 'BCDEF'
?>
Some other examples:
<?php
$search = array(' ', '&');
$replace = array(' ', '&');
$subject = 'Hello & goodbye!';
// We want to replace the spaces with and the ampersand with &
echo str_replace($search, $replace, $subject); // output: "Hello&nbsp&&nbspgoodbye!" - wrong!
echo stro_replace($search, $replace, $subject); // output: "Hello & goodbye!" - correct!
/*
Note: Run the above code in the CLI or view source on your web browser - the replacement strings for stro_replace are HTML entities which the browser interprets.
*/
?>
<?php
$search = array('ERICA', 'AMERICA');
$replace = array('JON', 'PHP');
$subject = 'MIKE AND ERICA LIKE AMERICA';
// We want to replace the name "ERICA" with "JON" and the word "AMERICA" with "PHP"
echo str_replace($search, $replace, $subject); // output: "MIKE AND JON LIKE AMJON", which is not correct
echo stro_replace($search, $replace, $subject); // output: "MIKE AND JON LIKE PHP", which is correct
?>
nospam at nospam dot com
02-Dec-2008 02:55
Replacement for str_replace in which a multiarray of numerically keyed data can be properly evaluated with the given template without having a search for 11 be mistaken for two 1's next to each other
<?php
function data_template($input, $template) {
if ($template) { // template string
if ($split = str_split($template)) { // each char as array member
foreach ($split as $char) { // each character
if (is_numeric($char)) { // test for digit
if ($s != 1) { // new digit sequence
$i++;
$s = 1;
}
$digits[$i] .= $char; // store digit
} else { // not a digit
if ($s != 2) { // new non-digit sequence
$i++;
$s = 2;
}
$strings[$i] .= $char; // store string
}
}
if ($i && $input && is_array($input)) { // input data
foreach ($input as $sub) { // each subarray
if (is_array($sub)) {
$out = ''; // reset output
for ($j = 0; $j <= $i; $j++) { // each number/string member
if ($number = $digits[$j]) { // number
$out .= $sub[$number]; // add value from subarray to output
} else { // string
$out .= $strings[$j]; // add to output
}
}
$a[] = $out;
}
}
return $a;
} // input
} // split
} // template
}
$input = array(array(1=>'yellow', 2=>'banana', 11=>'fruit'), array(1=>'green', 2=>'spinach', 11=>'vegetable'), array(1=>'pink', 2=>'salmon', 11=>'fish'));
print_r (data_template($input, '2: a 1, healthy 11'));
/*
Array
(
[0] => banana: a yellow, healthy fruit
[1] => spinach: a green, healthy vegetable
[2] => salmon: a pink, healthy fish
)
*/
// str_replace would have wanted to output 'banana: a yellow, healthy yellowyellow
?>
Not sure if this will help anyone but I wrote it for my application and thought I would share just in case
nick at NOSPAM dot pitchinteractive dot com
06-Oct-2008 04:12
I tried max at efoxdesigns dot com solution for str_replace_once but it didn't work quite right so I came up with this solution (all params must be strings):
<?php
function str_replace_once($search, $replace, $subject) {
$firstChar = strpos($subject, $search);
if($firstChar !== false) {
$beforeStr = substr($subject,0,$firstChar);
$afterStr = substr($subject, $firstChar + strlen($search));
return $beforeStr.$replace.$afterStr;
} else {
return $subject;
}
}
?>
paolo A T doppioclick D O T com
05-Sep-2008 04:15
For PHP 4 < 4.4.5 and PHP 5 < 5.2.1 you may occur (like me) in this bug:
http://www.php-security.org/MOPB/MOPB-39-2007.html
troy at troyonline dot com
22-Jun-2008 10:18
Yet another deep replace function:
<?php
function str_replace_deep( $search, $replace, $subject)
{
$subject = str_replace( $search, $replace, $subject);
foreach ($subject as &$value)
is_array( $value) and $value =str_replace_deep( $search, $replace, $value);
return $subject;
}
?>
David Gimeno i Ayuso (info at sima dot cat)
09-Aug-2007 12: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 )
?>
tim at hysniu dot com
05-Jun-2007 11: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
25-Feb-2007 05: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 12: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."
15-Jan-2007 01: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.
matt wheaton
30-Mar-2006 07: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);
?>
David Gimeno i Ayuso (info at sima-pc dot com)
25-Aug-2003 06: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".
