我正在将我发现的 eregi_replace 函数转换为 preg_replace,但 eregi 字符串几乎包含键盘上的每个字符。所以我尝试使用 £ 作为分隔符..它目前正在工作,但我想知道它是否可能会导致问题,因为它是一个非标准字符?
这是 eregi:
function makeLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a href="http://\\2">\\2</a>', $text);
return $text;}
和怀孕:
function makeLinks($text) {
$text = preg_replace('£(((f|ht){1}tp://)[-a-zA-^Z0-9@:%_\+.~#?&//=]+)£i',
'<a href="\\1">\\1</a>', $text);
$text = preg_replace('£([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)£i',
'\\1<a href="http://\\2">\\2</a>', $text);
return $text;
}
最佳答案
£
是有问题的,因为它不是 ASCII 字符。它来自 Latin-1 字符集,仅当您的 PHP 脚本也使用 8 位表示时才有效。如果您的文件编码为 UTF-8,则 £
将表示为两个字节。 PHP 中的 PCRE 会被它绊倒。 (至少我的版本是这样。)
关于php - 在 preg_replace 中使用 £ 作为定界符可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5207520/