PHP/String/substr replace

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

By supplying no start or length arguments, the string will be added to the beginning

   <source lang="html4strict">

<?php

 $mystring = substr_replace("Hello World", "H3110 W0r1d!", 0, 0);
 echo $mystring . "
"; //Echos H3110 W0r1d!Hello World $mystring = substr_replace("Hello World", "0 w0", 4, 4); echo $mystring; //Echos Hell0 w0rld.

?>

 </source>
   
  


PHP has two main functions for performing simple search-and-replace operations. The first one is substr_replace

   <source lang="html4strict">

<?php

   $haystack = "this is a test";
   $newstr = substr_replace ($haystack, "sad", 6, 5); 
   echo "$newstr\n";

?>

 </source>
   
  


Replacing a Portion of a String Using substr_replace()

   <source lang="html4strict">

<?

   $membership = "asdfasdf";
   $membership = substr_replace( $membership, "00", 2, 2);
   print "New membership number: $membership
";

?>

 </source>
   
  


Replacing a substring with substr_replace()

   <source lang="html4strict">

$new_string = substr_replace($old_string,$new_substring,$start); $new_string = substr_replace($old_string,$new_substring,$start,$length);

 </source>
   
  


Replacing Substrings: string substr_replace ( string str, string replacmnt, int start [, int len] )

   <source lang="html4strict">

<?php $mystring = substr_replace("Hello World", "asdf", 0, 0); echo $mystring . "
"; $mystring = substr_replace("Hello World", "0 w0", 4, 4); echo $mystring;

?>

 </source>
   
  


substr_replace() function replaces the string with replacement.

   <source lang="html4strict">

Its syntax is: string substr_replace (string string, string replacement, int start [, int length]) If start is positive, replacement starts at character start. If start is negative, replacement starts at (string length - start). If length is positive, replacement will be length characters long. If length is negative, replacement will end at (string length - length) characters. <? $favs = "is good"; $name = "A"; $favs = substr_replace($favs, $name, 0, 0); print $favs; ?>

 </source>
   
  


substr_replace.php

   <source lang="html4strict">

<?php

  $ccnumber = "1234567899991111";
  echo substr_replace($ccnumber,"************",0,12);

?>

 </source>