如何删除最后一个字符,只有当它是一个句号?

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

7 个解决方案

#1


131  

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

(Reference: rtrim on PHP.net)

(参考:在PHP.net上空白)

#2


26  

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

使用rtrim取代了所有"."在结尾,而不仅仅是最后一个字符

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

#3


6  

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.

要删除最后一个字符,仅当它是一个句点,而不是使用preg_replace,我们可以将该字符串视为一个char数组,如果是一个点,则删除最后一个字符。

if ($str[strlen($str)-1]==='.')
  $str=substr($str, 0, -1);

#4


1  

I know the question is some what old but may be my answer is helpful for someone.

我知道这个问题是什么年代的问题,但我的答案可能对某些人有帮助。

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

$ string =“一些东西..........”;

ltrim would remove leading dots. for example:- ltrim($string, ".")

ltrim会去除前导点。例如:ltrim(字符串,美元“。”)

rtrim rtrim($string, ".") would remove trailing dots.

rtrim($string, ".")将删除尾部的点。

trim trim($string, ".") would remove trailing and leading dots.

饰边($string, ".")将删除拖尾和前导点。

you can also do this by regex

您也可以通过regex进行此操作

preg_replace would remove can be used to remove dot/dots at the end

preg_replace将删除可用于删除端点上的点/点

$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.

我希望这对你有帮助。

#5


0  

Use a combination of strrpos and substr to get the position of the last period character and remove it leaving all other characters intact:

使用strrpos和substr的组合来获取最后一个周期字符的位置,并删除它,使所有其他字符保持不变:

$string = "something here.";

$pos = strrpos($string,'.');
if($pos !== false){
  $output = substr($string,0,$pos);
} else {
  $output = $string;
}

var_dump($output);

// $output = 'something here';

#6


0  

You can use rtrim function of php which allows you to trim the data which exist in last position.

您可以使用php的rtrim函数,它允许您修饰最后位置的数据。

For example :

例如:

$trim_variable= rtrim($any_string, '.');

Simplest and fasted way !!

最简单的方法!!

#7


-1  

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'

#1


131  

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

(Reference: rtrim on PHP.net)

(参考:在PHP.net上空白)

#2


26  

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

使用rtrim取代了所有"."在结尾,而不仅仅是最后一个字符

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

#3


6  

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.

要删除最后一个字符,仅当它是一个句点,而不是使用preg_replace,我们可以将该字符串视为一个char数组,如果是一个点,则删除最后一个字符。

if ($str[strlen($str)-1]==='.')
  $str=substr($str, 0, -1);

#4


1  

I know the question is some what old but may be my answer is helpful for someone.

我知道这个问题是什么年代的问题,但我的答案可能对某些人有帮助。

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

$ string =“一些东西..........”;

ltrim would remove leading dots. for example:- ltrim($string, ".")

ltrim会去除前导点。例如:ltrim(字符串,美元“。”)

rtrim rtrim($string, ".") would remove trailing dots.

rtrim($string, ".")将删除尾部的点。

trim trim($string, ".") would remove trailing and leading dots.

饰边($string, ".")将删除拖尾和前导点。

you can also do this by regex

您也可以通过regex进行此操作

preg_replace would remove can be used to remove dot/dots at the end

preg_replace将删除可用于删除端点上的点/点

$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.

我希望这对你有帮助。

#5


0  

Use a combination of strrpos and substr to get the position of the last period character and remove it leaving all other characters intact:

使用strrpos和substr的组合来获取最后一个周期字符的位置,并删除它,使所有其他字符保持不变:

$string = "something here.";

$pos = strrpos($string,'.');
if($pos !== false){
  $output = substr($string,0,$pos);
} else {
  $output = $string;
}

var_dump($output);

// $output = 'something here';

#6


0  

You can use rtrim function of php which allows you to trim the data which exist in last position.

您可以使用php的rtrim函数,它允许您修饰最后位置的数据。

For example :

例如:

$trim_variable= rtrim($any_string, '.');

Simplest and fasted way !!

最简单的方法!!

#7


-1  

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'
标签:

相关文章