I am trying to make a function that cuts the white spaces in a string and ads punctuation if there isn’t any.
This is my test string:
$test = 'Hey there I am traa la la ';
I want it to turn into this:
$test = 'Hey there I am traa la la.';
Here is the function I have tried:
function test($mytrim){
for($i = 0; $i <= 5; $i++){
if(substr($mytrim, 0, -1) == ''){
$mytrim = substr($mytrim, 0, -1);
}
}
$punct = array(".",",","?","!");
if(!in_array($mytrim, $punct)){ $mytrim .= '.'; }
return $mytrim;
}
It returns this:
$mytrim = 'Hey there I am traa la la. .';
Any ideas why it is not working?
PHP has a built in
trimfunction. as for the punctuations, your code should work fine (appending).Code sample :