我正在做一些數據掃描可能(或可能不會 - 視情況而定)在其文檔中出現正則表達式模式的文檔內容。PHP:如何匹配文檔中所有正則表達式模式的出現
我想編寫一個PHP函數來處理文檔 - 函數的工作是返回文檔中所有匹配模式聲音的數組(如果它們存在),或者返回一個空數組if沒有找到匹配。
我相信這涉及到使用PHP函數preg_match_all,但是,我不明白preg_match_all返回的數組的格式。我只是想匹配的字符串返回1維的(即非嵌套數組)如下:
<?php
$pattern = "^[h].[a-z]{3,4}";
$doc = file_get_contents('some_pathname');
function get_matching_patterns($pattern, $doc){
$out = array();
if (strlen($doc) && strlen($pattern)){
// not sure about this - I don't like the complicated nested array returned
// by preg_match-all
preg_match_all($pattern, $doc, $out);
}
return out;
}
?>
$ out($ missing)。嘗試print_r($ out)或var_dump。你會看到結構 – galchen
和** doc **和** pattern **缺少$以及 - > ** $ doc **,** $ pattern ** – abcde123483
對不起,來回切換問題太多的語言!有時候,我忘了PHP需要簽名!我將修復代碼片段。 –