|
Posted by Joe Estock on 12/18/60 11:50
Joe Estock wrote:
> J Huntley Palmer wrote:
>
>> How may I capture the last /.../ in a url?
>>
>> eg.. www.foo.com/foo/bar/baz/index.php?param=1
>>
>> I want to capture 'baz'.
>>
>> Thanks
>
>
> Start from the end of the string and work backwards until you hit the
> second /. Untested code follows:
>
> $flag = false;
> for ($i = strlen($url); $i > 0; $i--)
> {
> if ($url{$i} == '/')
> {
> if ($flag == true)
> {
> /* we have our match */
> $url = substr($url, $i);
> break;
> }
>
> /* got the first / */
> $flag = true;
> }
> }
Whoops, misread your intent. Corrections follow (again, untested).
$flag = false;
$j = 0;
for ($i = strlen($url); $i > 0; $i--)
{
if ($url{$i} == '/')
{
if ($flag == true)
{
/* we have our match */
$url = substr($url, $i, ($j - $i));
break;
}
/* got the first / */
$flag = true;
$j = $i;
}
}
Navigation:
[Reply to this message]
|