文章詳情頁
php - 讓網址變得更漂亮?
瀏覽:139日期:2022-06-11 14:06:27
問題描述
我想呈現中文網址然後把一些特殊字元轉換成「-」
$patten = array(’!’,’!’,’?’,’?’,’~’,’~’,’「’,’」’,’,’,’,’,’.’,’。’ );$zh_url = str_replace(’ ’,’’,str_replace($patten,’-’,strtolower($title)));
像這樣只是有時候會出現兩個 「--」如何讓字串中假設有兩個以上(含)「--」時,自動變成「一個」xx-x--xxxx--xxx變成xx-x-xxxx-xxx
然後假設字串最後面有一個以上的 「-」時,就把它去除?比如說 xxxxxxx-xxx-- 變成 xxxxxxx-xxx
問題解答
回答1:$zh_url = preg_replace(’#-{2,}#’, ’-’, trim(str_replace(’ ’, ’’, str_replace($patten, ’-’, strtolower($title))), ’-’));
update:
$zh_url = preg_replace(’#-{2,}#’, ’-’, preg_replace(’#^-{2,}|-{2,}$#’, ’’, str_replace(’ ’, ’’, str_replace($patten, ’-’, strtolower($title)))));
相關文章:
1. css3 - 沒明白盒子的height隨width的變化這段css是怎樣實現的?2. java - 根據月份查詢多個表里的內容怎么實現好?3. python3.x - c++調用python34. javascript - 在 model里定義的 引用表模型時,model為undefined。5. php工具中的mysql還是5.1以下的,請問如何才能升級到5.1以上?6. css3 - 這個右下角折角用css怎么畫出來?7. atom開始輸入!然后按tab只有空格出現沒有html格式出現8. javascript - 移動端自適應9. android - 課程表點擊后浮動后邊透明可以左右滑動的界面是什么?10. apache - 想把之前寫的單機版 windows 軟件改成網絡版,讓每個用戶可以注冊并登錄。類似 qq 的登陸,怎么架設服務器呢?
排行榜
