如何在 PHP 中删除字符串末尾的点 (.)

Chr*_*iso 2 php string replace

我有一个名为“5 Fragen an ... xyz”的字符串(wordpress 标题),在删除 HTML 标签后,该标题中的点应该被删除/替换。我除了字符串仅显示“xyz”

<?php
$wptitle = get_the_title($post->id); // getwordpress title
$wptitle = strip_tags($wptitle);    // remove a <br> tag
$wptitle = preg_replace('/\.(?=.*\.)/', ' ', $wptitle); // want to remove the dots
$wptitle = str_replace('5 Fragen an', '', $wptitle); // remove the part 5 Fragen an
echo $wptitle
?>
Run Code Online (Sandbox Code Playgroud)

jsp*_*pit 7

使用rtrim删除 PHP 中字符串末尾的点 (.) 或其他字符。使用character_mask参数,您可以指定要在末尾删除的字符。

带点的示例:

$string = "remove dots (.) at the end of an string ...";
$string = rtrim($string,'.');
var_dump($string);
//string(40) "remove dots (.) at the end of an string "
Run Code Online (Sandbox Code Playgroud)

这就是标题中问题的答案。

更新:

如果输入字符串看起来像这样

$input = "5 Fragen an ... xyz some new title for blog ";
Run Code Online (Sandbox Code Playgroud)

您可以执行此操作以提取点后的字符串:

$title = ltrim(strpbrk($input , "." ),'. ');

echo $title; //xyz some new title for blog
Run Code Online (Sandbox Code Playgroud)