★ wanayoo — archive 1999 http://www.php.net/manual/function.str-replace.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: str_replace
View the source code for this pageSearch the site



Previous page
 strtoupper
 Updated
Sat, 12 Aug 2000
strtr 
Next page


str_replace

(PHP3 >= 3.0.6, PHP4 )

str_replace --  Replace all occurrences of needle in haystack with str

Description

string str_replace (string needle, string str, string haystack)

This function replaces all occurences of needle in haystack with the given str. If you don't need fancy replacing rules, you should always use this function instead of ereg_replace().

Example 1. Str_replace() example


$bodytag = str_replace ("%body%", "black", "<body text=%body%>");
      

This function is binary safe.

Note: Str_replace() was added in PHP 3.0.6, but was buggy up until PHP 3.0.8.

See also ereg_replace() and strtr().


User Contributed Notes: str_replace


light@buypages.com
14-Jan-2000 12:40
It would be nice to have a stri_replace which would do a case-insensitive find on the needle string


mwigge@trans-it.de
02-Mar-2000 08:55
To replace a double backslash:
$string = str_replace("\\\\","\\",$string);



joelgg@mindspring.com
09-Mar-2000 02:57
I whole heartedly agree with the first comment! Until that function comes out, I'm stuck with using eregi() which is horrendously slow! (Especially when you are working with 100,000 or so strings to parse.)


kris@mha.ca
24-Mar-2000 03:42
Preg_replace is almost as fast as str_replace for simple strings... Use
<PRE>$bodytag = preg_replace("/%body%/i", "black", "<body text=%body%>");</PRE>



paul_kroll@hotmail.com
26-Mar-2000 05:01
Benchmarking using PHP 3.0.15, preg_replace is NOT "about as fast" as str_replace. str_replace is about 25% faster on a single match in a short string, and gets increasingly faster as the number of matches increases. (It hits 38% on the second match...) Whole-script times for 10000 matches in a loop only showed 14% differences in runtime: if you're running a script that does ONE match as a test, you'll see absolutely no difference in runtime.

If you're doing a lot of matching, or if your server is heavily loaded, every little bit helps... and as a side note, doing case-insensitive preg_replace appears to be within 5% of case-sensitive preg_replace. (Case insensitive searching is almost always slower than case sensitive, it's just "by how much.")



styx@osx.endlos.at
17-Apr-2000 04:06
to delete everything between (font size=-1> and </font>

try something like
$data is the data
this is kinda slow :) and not optimized 2 anything..

<pre>$first=strpos($data,"nt>");
$last=strpos($data,"<",$first+1)-$first-3;
$output=htmlspecialchars(substr(($data),$first+4,$last));</pre>



&quot;Perrault Jean-Paul&quot; \npj22@cornell.edu
18-May-2000 04:51
\n =LF
\r =CR



dave@e-underground.net
29-Jun-2000 08:21
A way to remove multiple items without using
str_replace("","",(str_replace"","", $var)):

<?php
$patterns = array("/B/", "/D/" );
$replacement = array("-", "=");
print preg_replace($patterns, $replacement, "ABCDE");
?>

Hope that helps!



jarrod@optimusdesign.com
26-Jul-2000 10:14
I just wanted to give an example of how much faster str_replace is compared to ereg_replace. Using ereg_replace to parse a radius (username/password) file with about 1000 users took me 1 minute and 29 seconds. Using str_replace it took about 5 seconds.


markus.brachner@net-inside.at
02-Aug-2000 06:21
If you want to work around the thousands-separator-bug/feature where you can't format a number without thousand separators use
str_replace("A","",number_format($number,2,".","A"))



 About Notes


Previous page
 strtoupper
 Updated
Sat, 12 Aug 2000
strtr 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.