How to Remove a specific character from the end of the string in php
$string = 'سلام-تست-است-';
i want change like
$string = 'سلام-تست-است';
so end of the سلام-تست-است we have Extra characters " - " and i want to remove it.
and this is my code :
foreach($tag as $t){
$t = str_replace(' ', '-', $t);
if(substr($t, -1) == '-'){
$t = rtrim($t,"-");
}
$insert_value[] = '("'.$content_id.'","'.$t.'","'.time().'")';
}
$tag is my string . any idea?
Since rtrim
does nothing if the character at the end was not found, you can simply run it without the if
check:
foreach($tag as $t) {
$t = rtrim($t);
$t = str_replace(' ', '-', $t);
$insert_value[] = '("'.$content_id.'","'.$t.'","'.time().'")';
}
Or even more reduced to:
foreach($tag as $t) {
$t = str_replace(' ', '-', rtrim($t));
$insert_value[] = '("'.$content_id.'","'.$t.'","'.time().'")';
}
However, this is just a hint to simplify your code. It should also work in it's current form as shown in the question, meaning the problem seems to be somewhere else.