I would like to delete the last character of a string if it is a symbol. I know I could do it in a complex way like the following function:
$string = "minha_string_com_simbolo_no_final_";
function excluir($string){
$simbolos = ["'","\"", "@","#","$","%","&","*", "(",")","-","_","+","=","
[","]","§","ª","º","{","}","´","'","^","~","?","/",";",":",",",".",",
",">","\","|","¬","¢","£"];
if(array_search(substr($string, -1), $simbolos)){
return substr_replace($string, '', -1);
}
}
Or extremely simple as the solution below with regular expression:
$string = "minha_string_com_simbolo_no_final_";
print preg_replace("/\W$/", "", $string);
Which option has a better performance?
What is the correct way to do beanckmark tests in PHP when we come across these cases?