我知道有人问before,但我仍然无法理解为什么以下内容不起作用:
<?php
echo preg_replace('/(?:[?\p{L}]*\/)?(?:[?\w\-]*\/)?/', '$1', 'cafes/123456') . '<br />';
// '123456'
echo preg_replace('/(?:[?\p{L}]*\/)?(?:[?\w\-]*\/)?/', '$1', 'cafés/123456') . '<br />';
// 'café123456' where I expect 123456
echo preg_replace('/(?:[?\p{L}é]*\/)?(?:[?\w\-]*\/)?/', '$1', 'cafés/123456') . '<br />';
// '123456' thanks to the extra "é" after "\p{L}"
?>
是不是\p{L}
应该替换任何重音?
答案 0 :(得分:2)
您缺少将字符串视为UTF-8的u
modifier:
echo preg_replace('/(?:[?\p{L}]*\/)?(?:[?\w\-]*\/)?/u', '$1', 'cafes/123456') . '<br />';
// '123456'
echo preg_replace('/(?:[?\p{L}]*\/)?(?:[?\w\-]*\/)?/u', '$1', 'cafés/123456') . '<br />';
// '123456'
echo preg_replace('/(?:[?\p{L}é]*\/)?(?:[?\w\-]*\/)?/u', '$1', 'cafés/123456') . '<br />';
// '123456'