PHP - 如何删除字符串末尾的所有特定字符?

时间:2010-01-13 01:40:37

标签: php regex string

如果只是一段时间,如何删除最后一个字符?

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

9 个答案:

答案 0 :(得分:142)

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

(参考:rtrim on PHP.net

答案 1 :(得分:27)

使用rtrim替换所有“。”最后,不只是最后一个字符

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

答案 2 :(得分:6)

要删除最后一个字符,只要它是一个句点而不是+-------+-------------+------+-----------+-------------+ | ID | SNA_Name | Desc | Time | Name | +-------+-------------+------+-----------+-------------+ | 741 | Temp | NNE | 2015-01-.| GRFDSD14 | +-------+-------------+------+-----------+-------------+ ,我们可以将字符串视为char数组,如果它是一个点,则删除最后一个字符。

preg_replace

答案 3 :(得分:2)

我知道问题是一些旧的但可能是我的答案对某人有帮助。

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

ltrim会删除前导点。例如: - ltrim($string, ".")

rtrim rtrim($string, ".")会删除尾随点。

trim trim($string, ".")会删除尾随点和前导点。

你也可以通过正则表达式

来做到这一点

preg_replace将删除可用于删除末尾的点/点

$regex = "/\.$/"; //to replace single dot at the end
$regex = "/\.+$/"; //to replace multiple dots at the end
preg_replace($regex, "", $string);

我希望它对你有所帮助。

答案 4 :(得分:0)

使用strrpossubstr的组合来获取最后一个句点字符的位置并将其删除,其他所有字符保持不变:

$string = "something here.";

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

var_dump($output);

// $output = 'something here';

答案 5 :(得分:0)

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

例如:

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

最简单和禁食的方式!!

答案 6 :(得分:0)

可以用不同的方式删除最后一个字符,这是一些

  • rtrim():

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

  • 正则表达式

    preg_replace(“ /.$/”,“”,$ string);

  • substr()/ mb_substr()

    echo mb_substr($ string,0,-1);

    echo substr(trim($ string),0,-1);

  • 具有strim()的substr()

    echo substr(trim($ string),0,-1);

答案 7 :(得分:0)

我知道问题已经解决。但是也许这个答案对某人会有帮助。

rtrim()-从字符串末尾去除空格(或其他字符)

ltrim()-从字符串开头去除空格(或其他字符)

trim()-从字符串的开头和结尾去除空格(或其他字符)

要从字符串末尾删除特殊字符,或者是在字符串末尾包含动态特殊字符的情况下,我们可以通过正则表达式来完成。

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

这里是一些示例,用于了解将$regex = "/[.*?!@#$&-_ ]+$/";应用于字符串

$string = "Some text........"; // $resul -> "Some text";
$string = "Some text.????";    // $resul -> "Some text";
$string = "Some text!!!";      // $resul -> "Some text";
$string = "Some text..!???";   // $resul -> "Some text";

希望对您有帮助。

谢谢:-)

答案 8 :(得分:-1)

实施例:     

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

?>

输出:

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'
相关问题