I'm creating a function that gets the last "word" of a url requested in php, without considering parameters and considering the root as index.
Examples:
URL link EXPECTATION index
URL www.teste.com.br/ EXPECTATION index
URL test.com EXPECTATION index
URL teste.com/ EXPECTATION index
URL www.teste.com.br/test EXPECTATION test
URL link EXPECTATION test
URL link EXPECTATION test
URL link EXPECTATION test
URL link EXPECTATION test
URL test.com/test/two EXPECTATION two
URL teste.com/teste/dois/ EXPECTATION two
URL teste.com/teste/dois/?variavel=teste EXPECTATION two
URL teste.com/teste/dois?variavel=teste EXPECTATION two
URL teste.com/teste/dois/?variavel=teste EXPECTATION two
URL test.com/teste?var1=t&var2=t EXPECTATION test
URL teste.com/teste/tres#ola EXPECTATIVE three
URL test.com/teste?var1=t&var2=t#ola EXPECTATION test
Using the basename
function and working with substr
and preg_match
I get a certain success rate:
$arr = array(
array("name"=>"http://www.teste.com.br/","possibleValues"=>array("index")),
array("name"=>"www.teste.com.br/","possibleValues"=>array("index")),
array("name"=>"teste.com","possibleValues"=>array("index")),
array("name"=>"teste.com/","possibleValues"=>array("index")),
array("name"=>"www.teste.com.br/teste","possibleValues"=>array("teste")),
array("name"=>"http://www.teste.com.br/teste","possibleValues"=>array("teste")),
array("name"=>"http://teste.com/teste","possibleValues"=>array("teste")),
array("name"=>"https://www.teste.com/teste","possibleValues"=>array("teste")),
array("name"=>"https://teste.com/teste","possibleValues"=>array("teste")),
array("name"=>"teste.com/teste/dois","possibleValues"=>array("dois")),
array("name"=>"teste.com/teste/dois/","possibleValues"=>array("dois")),
array("name"=>"teste.com/teste/dois/?variavel=teste","possibleValues"=>array("dois")),
array("name"=>"teste.com/teste/dois?variavel=teste","possibleValues"=>array("dois")),
array("name"=>"teste.com/teste/dois/?variavel=teste","possibleValues"=>array("dois")),
array("name"=>"teste.com/teste?var1=t&var2=t","possibleValues"=>array("teste")),
array("name"=>"teste.com/teste/tres#ola","possibleValues"=>array("tres")),
array("name"=>"teste.com/teste?var1=t&var2=t#ola","possibleValues"=>array("teste"))
);
foreach($arr as $value){
echo "URL ".$value["name"]."\n";
echo ( array_search( basename( returnLastWord( $value["name"] ) ), $value["possibleValues"] ) === false ? "FALHOU" : "PASSOU" )." -> expected: ".json_encode( $value["possibleValues"] )." get '".basename( returnLastWord( $value["name"] ) )."'\n\n";
}
function returnLastWord($var){
preg_match('/[?#]/', $var, $matches, PREG_OFFSET_CAPTURE);
$after = ( empty( $matches[0][1] ) ? NULL : $matches[0][1] );
if($after){
return substr($var, 0, $after);
}else{
// echo "aqui\n";
return $var;
}
}
URL http://www.teste.com.br/
FALHOU -> expected: ["index"] get 'www.teste.com.br'
URL www.teste.com.br/
FALHOU -> expected: ["index"] get 'www.teste.com.br'
URL teste.com
FALHOU -> expected: ["index"] get 'teste.com'
URL teste.com/
FALHOU -> expected: ["index"] get 'teste.com'
URL www.teste.com.br/teste
PASSOU -> expected: ["teste"] get 'teste'
URL http://www.teste.com.br/teste
PASSOU -> expected: ["teste"] get 'teste'
URL http://teste.com/teste
PASSOU -> expected: ["teste"] get 'teste'
URL https://www.teste.com/teste
PASSOU -> expected: ["teste"] get 'teste'
URL https://teste.com/teste
PASSOU -> expected: ["teste"] get 'teste'
URL teste.com/teste/dois
PASSOU -> expected: ["dois"] get 'dois'
URL teste.com/teste/dois/
PASSOU -> expected: ["dois"] get 'dois'
URL teste.com/teste/dois/?variavel=teste
PASSOU -> expected: ["dois"] get 'dois'
URL teste.com/teste/dois?variavel=teste
PASSOU -> expected: ["dois"] get 'dois'
URL teste.com/teste/dois/?variavel=teste
PASSOU -> expected: ["dois"] get 'dois'
URL teste.com/teste?var1=t&var2=t
PASSOU -> expected: ["teste"] get 'teste'
URL teste.com/teste/tres#ola
PASSOU -> expected: ["tres"] get 'tres'
URL teste.com/teste?var1=t&var2=t#ola
PASSOU -> expected: ["teste"] get 'teste'
I'm having problems especially in the first 4 examples, where theoretically it would be the root of the project, ie I should get the index