Snippets to manipulate PHP Strings
When I was creating a php-based program for the free web space that I offer to anyone who wants make your own website, I found that I often had to manipulate strings. In particular, it was useful to identify and, often remove, the first or last character of a string using PHP.(For a full look at PHP strings go to php.net/string)
Useful Code Snippets-- PHP Strings
Remove First Charcter of string
- $stringWithBadFirstLetter = '@otherwiseI'mAGreatString';
echo "$stringWithBadFirstLetter is the original string<br>";
$stringWithRemovedFirstLttr= substr($stringWithBadFirstLetter , 1 , (strlen($stringWithBadFirstLetter)-1));
echo " string using snippet above $stringWithBadFirstLetter";
Try if for yourself --> type in any string in the text box below or use the string generated for you and then watch the snippet in action
Although these functions are quite simple, what makes them nice is their generalized form. This generality is made possible by making use of the great strlen() feature of PHP.
Just as often, I found that the file management system for the web space that math warehouse hosts would need to remove the last character in a string.
How to Remove Last Charcter of string
- $stringwithBadLastChar = substr_replace($stringwithBadLastChar,"", -1);
Try if for yourself --> type in any string in the text box below or use the string generated for you and then watch this 1-line php script in action
"That's all for today, I will be leaving now with my hat which though not black is not exactly white"