1
I am creating a function that gets the last "word" of a requested url in php, without considering parameters and considering the root as index.
Examples:
URL http://www.teste.com.br/ EXPECTATION index
URL www.teste.com.br/ EXPECTATIVA index
URL test.with EXPECTATION index
URL test.com/ EXPECTATION index
URL www.teste.com.br/test EXPECTATION test
URL http://www.teste.com.br/test TEST EXPECTATION
URL http://test.com/test TEST EXPECTATION
URL https://www.teste.com/test TEST EXPECTATION
URL https://test.com/test TEST EXPECTATION
URL test.com/test/two EXPECTATION 2
URL test.com/test/two/ EXPECTATION TWO
URL test.com/test/two/? variable=test EXPECTATION two
URL test.com/test/two? variable=test EXPECTATION TWO
URL test.com/test/two/? variable=test EXPECTATION two
URL test.com/test? var1=t&var 2=t EXPECTATION test
URL test.com/test/tres#ola EXPECTATION
URL test.com/test? var1=t&var 2=t#ola EXPECTATION test
Using the function basename
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 trouble mainly in the first 4 examples, where theoretically it would be the root of the project, ie should get the index