PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




substr_replace

Name

substr_replace — Replaces part of a string with another string.

Synopsis

string substr_replace(string, replacement, start[, length]);
string string: String to search
string replacement: Replacement string
int start: Starting offset of the substring
int length (optional): Length of the substring

Returns

String

Description

substr_replace() is used to replace part of a string with another. The substring to be replaced is specified as a range using the start and length arguments. The string to replace this range of characters is specified with the replacement argument.

Behavior of the start and length arguments is interesting, allowing for a great deal of flexibility in choosing what part of the string to replace. See the following table for details.

Argument Value Behavior
start Non-negative integer Start the substring at start characters into the string.
start Negative integer Start the substring at start characters from the end of the string.
length Not set The substring extends from start to the end of the string.
length Non-negative integer End the substring length characters from the position specified by start .
length Negative integer End the substring length characters from the end of the string.

Caution

substr_replace() only operates in a left-to-right fashion. If start is a negative value and length is less than or equal to start , length is considered as having a value of0 instead. For example:

<?php
echo substr_replace('Oops!', 'X', -2, -4);
?>

Output:OopXs!




Version

PHP 4.0b4+

See also

Other methods of replacing characters in a string:

str_replace()

strtr()

The regular expression functions



Example

Example 1240. Demonstrate the behavior of substr_replace()

<?php
$string  = '0123456789ABCDEF';
$replace = 'X';

    // replace the first character
echo substr_replace($string, $replace, 0, 1), "\n",

     // replace the last character
     substr_replace($string, $replace, -1, 1), "\n",

    // insert a character in front of the last character
     substr_replace($string, $replace, -1, -3), "\n",

    // insert a character at the start of a string
     substr_replace($string, $replace, 0, 0), "\n";
?>

Output:
X123456789ABCDEF
0123456789ABCDEX
0123456789ABCDEXF
X0123456789ABCDEF

Example 1241. Replace the end of a long line with an ellipsis

<?php
$paragraph = <<<_Act_III_Scene_i_
   Good Master Mustardseed, I know your patience well:
   That same cowardly giant-like ox-beef hath devoured many a
   gentleman of your house: I promise you your kindred hath made my
   eyes water ere now. I desire you of more acquaintance, good
   Master Mustardseed.
_Act_III_Scene_i_;

foreach(explode("\n", $paragraph) as $line) {
   if (strlen($line) > 60)
      $line = substr_replace($line, "...", 57);

   echo $line,"\n";
}
?>

Output:   Good Master Mustardseed, I know your patience well:
   That same cowardly giant-like ox-beef hath devoured many a
   gentleman of your house: I promise you your kindred hath...
   eyes water ere now. I desire you of more acquaintance, good
   Master Mustardseed.



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.