note 39923 rejected from ref.strings by pollita

From: Date: Mon, 16 Feb 2004 00:32:47 +0000
Subject: note 39923 rejected from ref.strings by pollita
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-65187@lists.php.net to get a copy of this message
Note Submitter: sorxorith@kenetix.biz ---- I didn't see any way in PHP to append text to a string starting at a certain index. That is, rather than tacking it onto the end, have it tack the text on half-way through the string. Example: $src .= "Blah"; This works if you just want to add "Blah" to the end of $src. However, should you want to overwrite, say, the last character of $src with "Blah"... well that's what this is for. <?php // Function: str_append( string src, string newsrc[, int index] ) // Author: Xorith // Date: 02-15-04 // Description: // This will append newsrc to src, starting at index. If index is not specified, // the code will default to the end of src. The first two arguments are *required*. // Should src be empty, the function will just return newsrc. function str_append() { $args = func_get_args(); $cargs = func_num_args(); if( $cargs < 2 ) die( "Death!! str_append: required 2 args, $cargs provided!\n" ); if( empty( $args[0] ) || !isset( $args[0] ) ) return $args[1]; if( $cargs < 3 ) $args[2] = strlen( $args[0] ); for( $x = 0; $args[1][$x]; $x++, $args[2]++ ) $args[0][$args[2]] = $args[1][$x]; return $args[0]; } ?> Example: <?php $string = "Hello!"; echo $string; // Would give us "Hello!" $string = str_append( $string, " No!", 4 ); echo $string; // Would now be: "Hell No!" ?> Hope it helps someone. -- Xorith

« previous php.notes (#65187) next »