$removed_first_two = substr($sentence, 2) $removed_first_one = substr($sentence, 1) You can remove the first ‘n’ characters from a string by setting the value of start to ‘n’ in a similar manner. To remove the first two characters from the beginning, set the value of start to 2. To remove the first character from the beginning of string, set the value of start to 1. Omitting it will return the whole string starting from start index. The length parameter is optional in this function. You can also use the substr() function to remove characters from the beginning of a string by specifying a non-zero value for the length parameter. Output - An apple a day keeps anyone away if you tĮcho $removed_last_twenty Remove First ‘n’ Characters from a String Using substr() Output - An apple a day keeps anyone away if you throw it hard eno Output - An apple a day keeps anyone away if you throw it hard enou Output - An apple a day keeps anyone away if you throw it hard enoug Output - An apple a day keeps anyone away if you throw it hard enough $removed_last_twenty = substr($sentence, 0, -20) $removed_last_four = substr($sentence, 0, -4) $removed_last_three = substr($sentence, 0, -3) $removed_last_two = substr($sentence, 0, -2) $removed_last_one = substr($sentence, 0, -1) PHP $sentence = "An apple a day keeps anyone away if you throw it hard enough." In the following example, we have used the substr() function to remove different number of characters from the end of string. To put it simply, set the value of length property to be negative of the number of characters that you want to remove from the end of the string. Similarly, you can set the length to -2 in order to remove the last two characters from a string. You can set the value of length to -1 in order to remove the last character of a string. If you want to remove characters from the end of string, you can set the value of start to 0 and the value of length to a negative number. The PHP substr() function returns the part of string specified by the start and length parameters. Remove Last ‘n’ Characters from a String Using substr() Remove Specific Characters from the Beginning of String Using ltrim().Remove Specific Characters from the End of String Using rtrim().Remove First ‘n’ Characters from a String Using substr().Remove Last ‘n’ Characters from a String Using substr().
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |