简体   繁体   中英

Using Preg_replace for “…”

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 \… ):

$dir = str_replace("\xE2\x80\xA6", '', $dir);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM