您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關PHP如何使用preg_split()分割特殊字符的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
這里所說的特殊字符就是正則中使用的特殊字符,如: | . + 等
其它的先不說,來個實例:
$pattern="/[,-\\|\\.]/"; $subject="aaa,bbb,ccc-ddd-eee-fff|ggg|hhh.iii.jjj.kkk"; $spr=preg_split($pattern, $subject); print_r($spr);
結果:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] => )
很顯然,這不是我們想要的結果,郁悶了好一會,才找到是什么原因:
把正則表達式內的特殊字符放到前面就沒事了, 也就是
$pattern="/[\\|\\.,-]/";
結果:
Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd [4] => eee [5] => fff [6] => ggg [7] => hhh [8] => iii [9] => jjj [10] => kkk )
好了,這就是我們要的結果了
感謝各位的閱讀!關于“PHP如何使用preg_split()分割特殊字符”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。