Recipe 1.3. Replacing Substrings1.3.1. ProblemYou want to replace 1.3.2. SolutionUse Replacing a substring with substr_replace( )
1.3.3. DiscussionWithout the $length argument, substr_replace( ) replaces everything from $start to the end of the string. If $length is specified, only that many characters are replaced: print substr_replace('My pet is a blue dog.','fish.',12); If $start is negative, the new substring is placed at $start characters counting from the end of $old_string, not from the beginning: print substr_replace('My pet is a blue dog.','fish.',-9); If $start and $length are 0, the new substring is inserted at the start of $old_string: print substr_replace('My pet is a blue dog.','Title: ',0,0); The function substr_replace( ) is useful when you've got text that's too big to display all at once, and you want to display some of the text with a link to the rest. Example 1-16 displays the first 25 characters of a message with an ellipsis after it as a link to a page that displays more text. Displaying long text with an ellipsis
The more-text.php page referenced in Example 1-16 can use the message ID passed in the query string to retrieve the full message and display it. 1.3.4. See AlsoDocumentation on substr_replace( ) at http://www.php.net/substr-replace. |
Monday, January 25, 2010
Recipe 1.3. Replacing Substrings
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment