J Huntley Palmer wrote:[color=blue]
> 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[/color]
J Huntley Palmer wrote:[color=blue]
> 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[/color]
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;
}
Joe Estock wrote:[color=blue]
> J Huntley Palmer wrote:
>[color=green]
>> 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[/color]
>
>
> 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;
> }
> }[/color]
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;
}
J Huntley Palmer wrote:[color=blue]
> 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[/color]
Comment