How to remove first letter from a string using php - substr()
When you need to strip or remove the first letter from a string, the easiest way to do it is to use php substr(). you can use substr() for many purposes look at the php manual here
I needed this to construct a complicated MySQL query where the data input had to be really flexible, and the first character of the search term may or may not be available. This trick worked like a charm by letting me try the search with or without the first character. Thanks!
You can replace the first letter of a string by treating the string like an array with each letter being a key of the array. Counting in strings starts at 0. So in order to replace the first letter, you simply do this:
I have array of numbers (array(08056896869, 08099484948, 08067656789)) I wish to remove the first 0 from each of the numbers and have the array back. PHP scripter
Hi
ReplyDeleteUse substr($my_text,1,strlen($my_text)-1).
Sheshachaliah
thanks machan..it helped me. im new to PHP.
ReplyDeleteHi!
ReplyDeleteI want to replace first character of string with last character whatever it is.
Let say, I have a string:
$string="0123456";
I want it to be like this;
$string="612345";
and then
"51234"
and then
"4123" and so on.
How I can do that in php. I am new to php.Thanks
I needed this to construct a complicated MySQL query where the data input had to be really flexible, and the first character of the search term may or may not be available. This trick worked like a charm by letting me try the search with or without the first character. Thanks!
ReplyDeleteTo give an answer to the comment of Anonymous:
ReplyDeleteYou can replace the first letter of a string by treating the string like an array with each letter being a key of the array. Counting in strings starts at 0. So in order to replace the first letter, you simply do this:
$string = "abcdef";
$string[0] = "g";
echo $string; // will output "gbcdef"
Also you can try the following:
ReplyDeleteecho "z".substr($my_text,1,strlen($my_text)-1);
Thank you, very helpful
ReplyDeleteVictor
$value = "test content";
ReplyDelete$value = ltrim(rtrim($value, 't'), 't'); // est conten
I have array of numbers (array(08056896869, 08099484948, 08067656789))
ReplyDeleteI wish to remove the first 0 from each of the numbers and have the array back. PHP scripter