How do I remove all specific characters at the end of a string in PHP?

PhpRegexString

Php Problem Overview


How do I remove the last character only if it's a period?

$string = "something here.";
$output = 'something here';

Php Solutions


Solution 1 - Php

$output = rtrim($string, '.');

(Reference: rtrim on PHP.net)

Solution 2 - Php

using rtrim replaces all "." at the end, not just the last character

$string = "something here..";
echo preg_replace("/\.$/","",$string);

Solution 3 - Php

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);

Solution 4 - Php

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 :-)

Solution 5 - Php

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.

Solution 6 - Php

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() with trim()
echo substr(trim($string), 0, -1);

Solution 7 - Php

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 !!

Solution 8 - Php

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';

Solution 9 - Php

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

Solution 10 - Php

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongggggggggView Question on Stackoverflow
Solution 1 - PhpAlix AxelView Answer on Stackoverflow
Solution 2 - Phpghostdog74View Answer on Stackoverflow
Solution 3 - PhpC.O.View Answer on Stackoverflow
Solution 4 - PhpNishal K.RView Answer on Stackoverflow
Solution 5 - PhpShahbazView Answer on Stackoverflow
Solution 6 - PhpNishad UpView Answer on Stackoverflow
Solution 7 - PhpMoiz Shafqat HusainView Answer on Stackoverflow
Solution 8 - PhpGreensterRoxView Answer on Stackoverflow
Solution 9 - PhpMajbah HabibView Answer on Stackoverflow
Solution 10 - PhpChittaranjan SethiView Answer on Stackoverflow