Re: note 39923 rejected on strings
| From: | Xorith | Date: | Mon, 16 Feb 2004 19:31:50 +0000 |
| Subject: | Re: note 39923 rejected on strings | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-23504@lists.php.net to get a copy of this message | ||
Hi,
I can't find a place to lodge a complaint. My post wasn't, as the email
stated, requesting a feature OR reporting a bug. I simply provided a
function for other users incase they needed it as I did. To further that,
I tested it, unlike some of the self-proclaimed untested functions found
all over the site.
This, coupled with the way a few others in the PHP community have treated
me, is really leaving me bitter. :( Shame that such a strong language has
to have a community that doesn't quite support its users.
Copy of my note is below:
----- Copy of your note below -----
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