我想用preg_replace
清理一個字符串,但我想這個字符contine() - 圓括號的preg_replace有()個字符
我使用這個代碼
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
$string = preg_replace('/[^\p{Latin}\d ]/u', '', $string);
我想刪除除括號內的字母和數字
我想用preg_replace
清理一個字符串,但我想這個字符contine() - 圓括號的preg_replace有()個字符
我使用這個代碼
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
$string = preg_replace('/[^\p{Latin}\d ]/u', '', $string);
我想刪除除括號內的字母和數字
如果我理解正確的話,一切都使用:
/[^\p{Latin}0-9()]/u
這將匹配任何不是括號,字母或數字的內容。
所以全碼:
$string = preg_replace('/[^\p{Latin}0-9()]/u', '', $string);
當您使用像\ p {拉丁}這樣的unicode字符類或多字節文字字符時,請注意同時使用u修飾符,否則您的字符類將被視爲一組單字節而不是一組潛在的多字節字符,並可能導致不需要的行爲。 –
謝謝,很高興知道。我更新了答案。 – SuperDuperApps
你什麼意思,你想繼續用括號? – SuperDuperApps
是的,我想刪除除括號外的所有內容 –
使用'/ [^ characters] /'來匹配除指定字符以外的所有內容。 – Barmar