[英]How to remove dots (.) at the end of an string in PHP
I've got a string (wordpress title) called "5 Fragen an ... xyz" After I removed a HTML-Tag the dots in that title should be removed/replaced.我有一个名为“5 Fragen an ... xyz”的字符串(wordpress 标题)在我删除 HTML 标签后,该标题中的点应该被删除/替换。 I except the string displays 'xyz' only
我除了字符串只显示 '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
?>
Alright, First I had provided the bad answer. 好吧,首先,我提供了错误的答案。
So I made a script for your purpose. 因此,我为您编写了一个脚本。
// 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
Use rtrim to remove dots (.) or other chars at the end of an string in PHP. 使用rtrim在PHP字符串的末尾删除点(。)或其他字符。 With the character_mask parameter you can specify the characters you want to strip on end.
使用character_mask参数,您可以指定要结尾的字符。
Example with dots: 点示例:
$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 "
That's the answer to the question in the headline. 这就是标题中问题的答案。
Update: 更新:
If the input string looks like this 如果输入字符串看起来像这样
$input = "5 Fragen an ... xyz some new title for blog ";
you can do this for extract the string after the points: 您可以执行以下操作以提取点之后的字符串:
$title = ltrim(strpbrk($input , "." ),'. ');
echo $title; //xyz some new title for blog
The pattern that you tried \\.(?=.*\\.)
matches a dot \\.
您尝试
\\.(?=.*\\.)
与点\\.
匹配\\.
and uses a positive lookahead (?=
asserting that what is on the right is a dot. 并使用正向前瞻
(?=
断言右边是一个点。
In you example data, that will only match 2 out of 3 dots because for the last dot, the assertion will fail. 在您的示例数据中,这将仅匹配3个点中的2个,因为对于最后一个点,断言将失败。
If you want to match the last 1 or more consecutive dots, you could use a negative lookahead (?!
instead asserting what is on the right are no more dots following. 如果要匹配最后一个或多个连续的点,则可以使用负前瞻
(?!
而不是断言右边没有后面的点。
\.+(?!.*\.)
If you also want to match the horizontal whitespaces before and after the dots you could add using \\h*
如果您还想匹配点前后的水平空白,则可以使用
\\h*
添加
\h*\.+\h*(?!.*\.)
You code might look like: 您的代码可能看起来像:
$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
Note that in your code you use a space in the replacement for the dots. 请注意 ,在您的代码中,您用空格代替了点。
If you want to remove 5 Fragen an
including an n number of consecutive dots, you could use this pattern and replace with an empty string: 如果要删除
5 Fragen an
包括n个连续点),则可以使用此模式并替换为空字符串:
\b5 Fragen an\h+\.+\h+
There are numerous ways of doing this, this slightly re-organizes the version you have. 有许多方法可以这样做,这会稍微重新组织您拥有的版本。 So first remove the
'5 Fragen an'
part and then use ltrim()
to remove leading spaces and .
因此,首先删除
'5 Fragen an'
部分,然后使用ltrim()
删除前导空格和.
's... 的...
$wptitle = get_the_title($post->id); // getwordpress title
$wptitle = strip_tags($wptitle); // remove a <br> tag
$wptitle = str_replace('5 Fragen an', '', $wptitle);
$wptitle = ltrim($wptitle, " .");
echo $wptitle;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.