2
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 right way to test beanckmark in PHP when we come across these cases?
The two functions presented in the question are not equivalent. It would be good to clarify the "rules" of removal more accurately so that a satisfactory answer is possible. If you put in the post what is the real problem to be solved, it can be even more interesting.
– Bacco