Re: Checking if

From: Date: Wed, 12 Jan 2005 19:14:05 +0000
Subject: Re: Checking if
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-206237@lists.php.net to get a copy of this message
Bruno B B Magalhães wrote:
Richard, my solution right know is: if(substr($url,-1) != '/') {
    $url = $url.'/';
} Simple and fast... :)
$url = preg_replace ("|^(.*)/?$|", "\\1/", $url);
Regards, Bruno B B Magalhaes On Jan 12, 2005, at 3:37 PM, Richard Lynch wrote:
Bruno B B Magalhães wrote:
how to determine if the last char of a string is a '/'... The problem, a webpage can be accessed by www.domain.com/page.php or www.domain.com/page.php/
In addition to the two fine answers posted so far: if ($string[strlen($string)-1] == '/'){ echo "It ends in '/'<BR>\n"; } else{ echo "It does NOT end in '/'<BR>\n"; } substr and the above will be fastest, if it matters (probably not). The different in performance between substr and array reference is negligible, I think. preg_match is more flexible if you need to maybe some day figure out the last several letters in weird combinations. substr will be useful if you might some day need more letters, but not in weird combinations. The array usage may be more natural if you are already tearing apart the string character by character in other bits of the same code. YMMV -- Like Music? http://l-i-e.com/artists.htm
Cheers, -Morten

« previous php.general (#206237) next »