2

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. I except the string displays 'xyz' only

<?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
?>
Chriso
  • 39
  • 1
  • 1
  • 2
  • Is it always three dots? If so - something using `explode()` could do it. – Nigel Ren Aug 28 '19 at 15:28
  • The string displays "xyz" because you removed the rest of it with the `str_replace`. If you want to remove three dots, just do a str_replace on three dots. – FoulFoot Aug 28 '19 at 15:31
  • If it is ALWAYS three dots . $output = str_replace ('...','','mystring...'); – Will Aug 28 '19 at 15:31

3 Answers3

7

Use rtrim to remove dots (.) or other chars at the end of an string in PHP. With the character_mask parameter you can specify the characters you want to strip on end.

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
jspit
  • 7,276
  • 1
  • 9
  • 17
  • Another answer previously gave this same solution, but it was also pointed out that the `.`'s aren't at the end of the string. – Nigel Ren Aug 28 '19 at 18:13
2

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
Nemanja Jeremic
  • 334
  • 4
  • 18
1

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.

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.

\.+(?!.*\.)

Regex demo

If you also want to match the horizontal whitespaces before and after the dots you could add using \h*

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

Regex demo

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:

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

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70