oaziz
oaziz

Reputation: 1372

Replacing x or more dots

I want a clean solution to replace dots in text:

Some title.... to this: Some title...

Some.... title...... to this: Some... title...

How can I replace every sequence of more than 3 dots with 3 dots?

Upvotes: 0

Views: 2487

Answers (2)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57670

Its almost same as hakre. But more cleaner.

preg_replace('/\.\.\.+/', '...', $str);

Another repeated way (non-regex)

while(strpos($str, "....")!==false)
    $str = str_replace("....", "...", $str);

Upvotes: 1

hakre
hakre

Reputation: 198118

With a regular expression based search and replaceDocs:

$text = preg_replace('/\.{4,}/', '...', $text);

The pattern says: Match four or more dots ., the second parameter is the replacement.

Upvotes: 6

Related Questions