I want to be able to specify an index in a string and remove it.
I have the following:
"Hello World!!"
I want to remove the 4th index [o
in Hello]. Here would be the end result:
"Hell World!!"
I've tried unset[]
, but that hasn't worked. I've Googled how to do this and that's what everyone says, but it hasn't
worked for me. Maybe I wasn't using it right, idk.
asked Feb 23, 2013 at 3:40
anchortext to save proper content in the database.
Here is the implementation of offset factor:
$offset_factor = 0;
foreach[$content->links->links as $index=>$link]{
$replacement = ''.$link->anchorText.'';
$new_offset = $link->offset + $offset_factor;
$newtext = \substr_replace[$content->text, $replacement, $new_offset, $link->length];
//now we reset the original paragraph text with newtext
$content->text = $newtext;
//calculate the new offset by calculating the difference in replacement length and original length and add that to the offset_factor
$additional_characters = strlen[$replacement] - $link->length;
$offset_factor = $offset_factor + $additional_characters;
}
I hope this helps a noobie :] If there is another easier way, I would love to hear about it.
yeyijelud at amadamus dot com ¶
3 years ago
First Example can be simplified =>
$input = array['A: XXX', 'B: XXX', 'C: XXX'];
substr_replace[$input, 'YYY', -3];
output: Array [ [0] => A: YYY [1] => B: YYY [2] => C: YYY ]
NiX0n at fragfest dot cx ¶
13 years ago
The preemptive test to see if $string is "too long" shouldn't add strlen[$replacement] to $max. $max should represent the absolute maximum length of string returned. The size of the $replacement is irrelevant in that determination.
The rest of the function [unchanged below] operates as defined above. Meaning, the size of the $replacement is subtracted from the $max, so that the returned string is exactly the length of $max.
spcl dot delivery at gmail dot com ¶
14 years ago
the version of my predecessor will add $rep even if the string is shorter than max. fixed version:
To preserve the filename extension you can call it like this:
truncate[[filename], 30, '...' . end[explode['.', [filename]]]]
jaimthorn at yahoo dot com ¶
14 years ago
I recently needed a routine that would remove the characters in one string from another, like the regex
and I needed it to be fast, and accept pretty much all input. The regex above won't work when strlen[$chars] == 0. I came up with this, admittedly pretty horrible-looking code, that is quite fast:
According to my own measurements, the regex in ONLY faster for when strlen[$chars] == 1; for longer strings, my routine is faster. What does it do? Let's say you want to remove the period, the comma and the exclamation mark from a string, like so:
$result = RemoveChars["Isn't this, like, totally neat..!?", ".?!"];
The str_pad function creates a string equal in length to the string that contains the character to be removed, but consisting only of the first character of that string:
The input is ".,!"
The output is "..."
The strtr function translates all characters in the string-to-be-processed ["Isn't this..."] that also occur in the input [".,!"] to the characters in the same position in the output ["..."]. In other words:
Isn't this, like, totally neat..!?
becomes
Isn't this. like. totally neat....
Finally, the first character from the input [".,!"] which happens to be, again, the period, is removed from that string by the str_replace call:
Isn't this like totally neat?
The function needs to check is $chars has at least one character, or else the str_pad function will fail. If it's empty, then the unprocessed string is returned.
chuayw2000 at hotmail dot com ¶
16 years ago
I don't know if this function is multibyte safe but I've written a function that will do the same in multibyte mode.
Thijs Wijnmaalen [thijs[at]nllinux.nl] ¶
18 years ago
I wrote a function that you can use for example in combination with a search script to cut off the articles that are too long.
david at ethinkn dot com ¶
19 years ago
Here is a simple function to shorten a string and add an ellipsis
nospam at nospam dot com ¶
6 years ago
dmron ¶
18 years ago
Regarding "...", even the short functions are too long and complicated, and there's no need to use substr_replace. substr[] works better and is way faster prior to 4.3.5 as the below poster stated.
function shorten[ $str, $num = 100 ] {
if[ strlen[ $str ] > $num ] $str = substr[ $str, 0, $num ] . "...";
return $str;
}
Anonymous ¶
21 years ago
If you would like to remove characters from the start or end of a string, try the substr[] function.
For example, to remove the last three characters from a string:
$string = "To be or not to be.";
$string = substr [$string, 0, -3];
blessador at gmail dot com ¶
9 years ago