![]() ![]() ![]() Please show your love and support by sharing this post. To do so, you can do any of the following:Įcho ($url != '' & $url = '/') ? rtrimOnce($url) : $url Įcho (str_ends_with($url, '/')) ? rtrimOnce($url) : $url Įcho (substr($url, -1) = '/') ? rtrimOnce($url) : $url ![]() ' echo substr('Hello world',1,8).' ' echo substr('Hello world',0,5).' ' echo substr('Hello world',6,6).' ' echo substr('Hello world',0,-1).' ' echo substr('Hello world',-10,-2).' ' echo substr('Hello world',0,-6). You can use the following function to remove a single occurrence of the " /" character: However, you can update the pattern to remove multiple occurrences as well, for example, like so:Įcho preg_replace('//*$/', '', $url) // ''Ĭhecking the End Character and Removing Accordingly The above pattern would only only match and replace a single occurrence of " /" at the end of the string. Here is how to run it: Using pregreplace And the last handy function to use for removing the last character of a string is pregreplace (): Describing the Itrim Function The Itrim () function is supported on PHP 4, PHP 5, and PHP 7 versions.Use the negative length to omit a length number of characters in the returned substring. The last character in the input string has an index of -1. Use the negative offset to extract a substring from the end of the string. For example:Įcho preg_replace('//$/', '', $url) // '' Use the PHP substr () function to extract a substring from a string. Syntax: The syntax of subster method is given below: substr (string) Example1 Method First substr function string 'Hello World' echo 'Given string: '. You can use a regular expression with the preg_replace() function to remove a trailing slash without the need to first explicitly check if a trailing slash exists and then remove it. Method 1: substr function You can use the substr function of PHP for remove the first character from string in PHP. The rtrim() function matches and removes multiple occurrences of the " /" character: You can specify the " /" character as the second the argument to the rtrim() function to remove one or more trailing slashes, for example, like so:
0 Comments
Leave a Reply. |