I want to replace all the "..." with preg_replace, Example : Hello... => Hello

I tryed with :

$dir = preg_replace('/.../', '', $dir);

But it doesn't work :/

有帮助吗?

解决方案

The dot (.) is a special character in regular expressions which matches any single character except a newline (or any character at all if PCRE_DOTALL is specified). If you really want to match a literal . character you'd have to escape it or wrap it in a character class.

However, for something this simple, there's no need for regular expressions. Just use str_replace:

$dir = str_replace('...', '', $dir);

Also note, that a horizontal ellipsis () is a separate Unicode character from three periods (...). If you need to handle these as well, it gets kind of tricky since PHP doesn't offer true Unicode support. Exactly how you handle it depends on the encoding of $dir, but assuming it's UTF-8 encoded, this will remove any horizontal ellipsis characters (\xE2\x80\xA6 is the UTF-8 encoded form of \u2026):

$dir = str_replace("\xE2\x80\xA6", '', $dir);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top