PHP/String/str replace

Материал из Web эксперт
Перейти к: навигация, поиск

case-sensitive?

   <source lang="html4strict">

<?

   $string = "this is a test";
   $newstring = str_replace("test", "TEST", $string);
   print $newstring;

?>

 </source>
   
  


mixed str_replace ( mixed needle, mixed replace, mixed haystack [, int &count] )

   <source lang="html4strict">

<?

   $string = "this is a test";
   $newstring = str_replace("is", "is not", $string);
   print $newstring;

?>

 </source>
   
  


Replacing Substrings Using str_replace()

   <source lang="html4strict">

str_replace() replaces all instances of a string within another string.

<?php

   $string = "Site contents copyright 2003.";
   $string .= "The 2003 Guide";
   print str_replace("2003","2004",$string);

?>

 </source>
   
  


Replacing Tabs with Spaces

   <source lang="html4strict">

<?php $str = " \tline \t\tline \t\t\tline \t\t} \t} "; $replaced = str_replace("\t", " ", $str);

echo "
{$replaced}
";

?>

 </source>
   
  


str_replace() accepts arrays for all its arguments

   <source lang="html4strict">

<?php $source = array("a","b" ); $search = array( "a", "2000" ); $replace = array ( "b", "2001" ); $source = str_replace( $search, $replace, $source ); foreach ( $source as $str )

 print "$str
";

?>

 </source>
   
  


str_replace demo

   <source lang="html4strict">

<?php

   $haystack = "this is a test";
   $newstr = str_replace ("is", "is not", $haystack); 
   echo "$newstr\n";

?>

 </source>
   
  


str_replace() function searches for occurrence in string, replacing all instances with replacement.

   <source lang="html4strict">

Its syntax is: string str_replace (string occurrence, string replacement, string string) <? $favorite_food = "ice, cream, chicken wings"; $favorite_food = str_replace("chicken wings", "pizza", $favorite_food); ?>

 </source>
   
  


str_replace.php

   <source lang="html4strict">

<?php

  $author = "jason@example.ru";
  $author = str_replace("@","(at)",$author);
  echo "Contact $author.";

?>

 </source>
   
  


Switching tabs and spaces

   <source lang="html4strict">

<?php $tabbed = str_replace(" ","\t","this is a test"); $spaced = str_replace("\t"," ","this is a test");

print "With Tabs:
$tabbed
"; print "With Spaces:
$spaced
";

?>

 </source>
   
  


Using str_replace()

   <source lang="html4strict">

<? print str_replace("{class}",$my_class,

                 "A
                  B");

?>

 </source>
   
  


Using template file

   <source lang="html4strict">

<html> <head><title>{title}</title></head> <body>

{headline}

By {byline}

{article}


Page generated: {date}

</body> </html> //////////////////////// <?php $page = file_get_contents("article.html"); if ($page === false) {

   die("Can"t read article.html: $php_errormsg");

} $vars = array("{title}" => "A",

             "{headline}" => "B",
             "{byline}" => "C",
"{article}" => "

D

",
             "{date}" => date("l, F j, Y"));

foreach ($vars as $field => $new_value) {

   $page = str_replace($field, $new_value, $page);

} $result = file_put_contents("dog-article.html", $page); if (($result === false) || ($result == -1)) {

   die("Couldn"t write dog-article.html: $php_errormsg");

} ?>

 </source>