我試圖解析一個查詢字符串,並遇到了一些我無法弄清楚的東西。在php中遇到小於(<)的操作符preg_match_all
試圖匹配一個字符串,該字符串可以包含<或>,但是隻有當字符串包含<時,纔會看到一些奇怪的輸出。
例子:
<?php
$gt = 'wt>f';
preg_match_all("/([A-Za-z<>]+)/", $gt, $gtmatches);
?>
<pre><?php print_r($gtmatches); ?></pre>
返回預期:
Array
(
[0] => Array
(
[0] => wt>f
)
[1] => Array
(
[0] => wt>f
)
)
但只需更換該>與<,給人很奇怪輸出:
代碼:
<?php
$lt = 'wt<f';
preg_match_all("/([A-Za-z<>]+)/", $lt, $ltmatches);
?>
<pre><?php print_r($ltmatches); ?></pre>
輸出:
Array
(
[0] => Array
(
[0] => wt Array
(
[0] => wt
我測試過這對PHP 5.6.24/IIS 10,並在本地上的PHP 32年6月5日/阿帕奇2.4.29。
解決由於iainn(該死,你們是快!)
我的瀏覽器被解釋<作爲標籤的開始,望着源,一切都很好。
它適用於我: - https://eval.in/926067和https://eval.in/926069(兩個版本) –