PHP/String/substr replace
Содержание
- 1 By supplying no start or length arguments, the string will be added to the beginning
- 2 PHP has two main functions for performing simple search-and-replace operations. The first one is substr_replace
- 3 Replacing a Portion of a String Using substr_replace()
- 4 Replacing a substring with substr_replace()
- 5 Replacing Substrings: string substr_replace ( string str, string replacmnt, int start [, int len] )
- 6 substr_replace() function replaces the string with replacement.
- 7 substr_replace.php
By supplying no start or length arguments, the string will be added to the beginning
<?php
$mystring = substr_replace("Hello World", "H3110 W0r1d!", 0, 0);
echo $mystring . "<br />"; //Echos H3110 W0r1d!Hello World
$mystring = substr_replace("Hello World", "0 w0", 4, 4);
echo $mystring; //Echos Hell0 w0rld.
?>
PHP has two main functions for performing simple search-and-replace operations. The first one is substr_replace
<?php
$haystack = "this is a test";
$newstr = substr_replace ($haystack, "sad", 6, 5);
echo "$newstr\n";
?>
Replacing a Portion of a String Using substr_replace()
<?
$membership = "asdfasdf";
$membership = substr_replace( $membership, "00", 2, 2);
print "New membership number: $membership<br/>";
?>
Replacing a substring with substr_replace()
$new_string = substr_replace($old_string,$new_substring,$start);
$new_string = substr_replace($old_string,$new_substring,$start,$length);
Replacing Substrings: string substr_replace ( string str, string replacmnt, int start [, int len] )
<?php
$mystring = substr_replace("Hello World", "asdf", 0, 0);
echo $mystring . "<br />";
$mystring = substr_replace("Hello World", "0 w0", 4, 4);
echo $mystring;
?>
substr_replace() function replaces the string with replacement.
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;
?>
substr_replace.php
<?php
$ccnumber = "1234567899991111";
echo substr_replace($ccnumber,"************",0,12);
?>