How do I remove all specific characters at the end of a string in PHP?
How do I remove the last character only if it's a period?
$string = "something here.";
$output = 'so开发者_开发问答mething here';
$output = rtrim($string, '.');
(Reference: rtrim on PHP.net)
using rtrim replaces all "." at the end, not just the last character
$string = "something here..";
echo preg_replace("/\.$/","",$string);
To remove the last character only if it's a period and not resorting to preg_replace
we can just treat the string as an array of char and remove the final character if it is a dot.
if ($str[strlen($str)-1]==='.')
$str=substr($str, 0, -1);
I know the question is solved. But maybe this answer will be helpful for someone.
rtrim()
- Strip whitespace (or other characters) from the end of a string
ltrim()
- Strip whitespace (or other characters) from the beginning of a string
trim()
- Strip whitespace (or other characters) from the beginning and end of a string
For removing special characters from the end of the string or Is the string contains dynamic special characters at the end, we can do by regex.
preg_replace
- Perform a regular expression search and replace
$regex = "/\.$/"; //to replace the single dot at the end
$regex = "/\.+$/"; //to replace multiple dots at the end
$regex = "/[.*?!@#$&-_ ]+$/"; //to replace all special characters (.*?!@#$&-_) from the end
$result = preg_replace($regex, "", $string);
Here is some example to understand when $regex = "/[.*?!@#$&-_ ]+$/";
is applied to string
$string = "Some text........"; // $resul -> "Some text";
$string = "Some text.????"; // $resul -> "Some text";
$string = "Some text!!!"; // $resul -> "Some text";
$string = "Some text..!???"; // $resul -> "Some text";
I hope it is helpful for you.
Thanks :-)
I know the question is some what old but may be my answer is helpful for someone.
$string = "something here..........";
ltrim would remove leading dots. for example:- ltrim($string, ".")
rtrim rtrim($string, ".")
would remove trailing dots.
trim trim($string, ".")
would remove trailing and leading dots.
you can also do this by regex
preg_replace would remove can be used to remove dot/dots at the end
$regex = "/\.$/"; //to replace single dot at the end
$regex = "/\.+$/"; //to replace multiple dots at the end
preg_replace($regex, "", $string);
I hope it is helpful for you.
The last character can be removed in different ways, Here are some
rtrim()
$output = rtrim($string, '.');
Regular Expression
preg_replace("/\.$/", "", $string);
substr()
/mb_substr()
echo mb_substr($string, 0, -1);
echo substr(trim($string), 0, -1);
substr()
withtrim()
echo substr(trim($string), 0, -1);
You can use rtrim function of php which allows you to trim the data which exist in last position.
For example :
$trim_variable= rtrim($any_string, '.');
Simplest and fasted way !!
Use a combination of strrpos and substr to get the position of the last period character and remove it leaving all other characters intact:
$string = "something here.";
$pos = strrpos($string,'.');
if($pos !== false){
$output = substr($string,0,$pos);
} else {
$output = $string;
}
var_dump($output);
// $output = 'something here';
The chop() function removes whitespaces or other predefined characters from the right end of a string
$string = "something here.";
$string = chop($string,".");
echo $string;
Output
something here
Example:
$columns = array('col1'=> 'value1', 'col2' => '2', 'col3' => '3', 'col4' => 'value4');
echo "Total no of elements: ".count($columns);
echo "<br>";
echo "----------------------------------------------<br />";
$keys = "";
$values = "";
foreach($columns as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
$keys = $keys."'".$x."',";
$values = $values."'".$x_value."',";
echo "<br>";
}
echo "----------------------Before------------------------<br />";
echo $keys;
echo "<br />";
echo $values;
echo "<br />";
$keys = rtrim($keys, ",");
$values = rtrim($values, ",");
echo "<br />";
echo "-----------------------After-----------------------<br />";
echo $keys;
echo "<br />";
echo $values;
?>
Output:
Total no of elements: 4
----------------------------------------------
Key=col1, Value=value1
Key=col2, Value=2
Key=col3, Value=3
Key=col4, Value=value4
----------------------Before------------------------
'col1','col2','col3','col4',
'value1','2','3','value4',
-----------------------After-----------------------
'col1','col2','col3','col4'
'value1','2','3','value4'
精彩评论