【问题标题】:How to remove dots (.) at the end of an string in PHP如何在PHP中删除字符串末尾的点(。)
【发布时间】:2020-01-01 21:02:39
【问题描述】:

我有一个名为“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
?>

【问题讨论】:

  • 总是三个点吗?如果是这样 - 使用 explode() 的东西可以做到。
  • 字符串显示“xyz”,因为您使用str_replace 删除了它的其余部分。如果要删除三个点,只需在三个点上执行 str_replace 即可。
  • 如果总是三个点。 $output = str_replace('...','','mystring...');

标签: php string replace


【解决方案1】:

好的,首先我提供了错误的答案。

所以我为你的目的制作了一个脚本。

// function description here 
// https://stackoverflow.com/questions/15944824/search-a-php-array-for-partial-string-match/15944913#15944913
function array_search_partial(string $keyword, array $arr): int {
    foreach($arr as $index => $string) {
        if ( strpos($string, $keyword) !== false ) {
            return $index;
        }
    }
}

function NewTitle(string $string): string{

    // Here we split string into an array, and glue is " "
    $string = explode(' ', $string);

    // Now we search for a dots in array and $count getting number of key where is the dots
    $count = array_search_partial("..", $string);

    // Because we found a dots on some key (Let's say 3 key),
    // We shift the array because we don't need first words and dots in our title
    // Because the dots are on key 3 we must then shift 4 times array to remove dots and unnecessary words
    for($i=0;$i < $count+1;$i++){
        array_shift($string);
    }

    // Here we join array elements in a string with a " " glue
    $string = implode(' ', $string);

    // returning an new string
    return $string;
}


echo NewTitle("5 Fragen an ... xyz some new title for blog "); 
//OutPut: xyz some new title for blog

【讨论】:

  • 看起来句号实际上并不在字符串的末尾。
  • 那是我的错,我会在几分钟内提供一个更好的答案。
  • @GrumpyCrouton 我修好了
  • 你有一个函数声明 inside 另一个函数?
  • 在我看来,您的解决方案似乎有点矫枉过正。我删除了我的反对票,但我不相信它欠赞成票。如果你描述你的代码实际在做什么,也许这会改变,为什么这个解决方案是有意义的。
【解决方案2】:

使用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 "

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

更新:

如果输入字符串是这样的

$input = "5 Fragen an ... xyz some new title for blog ";

您可以这样做以提取点后的字符串:

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

echo $title; //xyz some new title for blog

【讨论】:

  • Another answer之前给出了相同的解决方案,但也有人指出.不在字符串的末尾。
【解决方案3】:

您尝试的模式 \.(?=.*\.) 匹配一个点 \. 并使用正向前瞻 (?= 断言右边是一个点。

在您的示例数据中,它将仅匹配 3 个点中的 2 个,因为对于最后一个点,断言将失败。

如果您想匹配最后 1 个或多个连续点,您可以使用否定的前瞻 (?!,而不是断言右侧没有更多点。

\.+(?!.*\.)

Regex demo

如果您还想匹配点前后的水平空格,您可以使用\h*添加

\h*\.+\h*(?!.*\.)

Regex demo

您的代码可能如下所示:

$wptitle = get_the_title($post->id); // getwordpress title
$wptitle = strip_tags($wptitle);    // remove a <br> tag
$wptitle = preg_replace('/\h*\.+\h*(?!.*\.)/', ' ', $wptitle);
$wptitle = str_replace('5 Fragen an', '', $wptitle);
echo $wptitle

注意,在您的代码中,您使用空格代替点。

如果要删除包含 n 个连续点的 5 Fragen an,可以使用此模式并替换为空字符串:

\b5 Fragen an\h+\.+\h+

Regex demo

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多