簡體   English   中英

preg_match_all php插入結果

[英]preg_match_all php inserting results

下面的代碼搜索一個很長的查詢結果“ body”,並提取引用的數字。 我想將這些數字插入到單列參考表中。 當它是一個preg_match時,我已經進行了此工作,但是preg_match都投入了精力,並且繼續困擾着數組字符串轉換。 每個“ body”結果可以有多個數字結果,並且我相信這會導致多個數組。 誰能幫我插入這個?

$textToReplace = mysqli_query($conn, "SELECT body from T1");
while($row2 = $textToReplace->fetch_array()){
    $body = $row2["body"];

    preg_match_all('#"\d+"#', $body, $matches);

    foreach($matches as $match) {
        $id=$match;
        var_dump($id);
        // $sql = mysqli_query($conn, "INSERT into T2 VALUES($id)");
    }
}

這是$ id上var_dump的結果的摘要:

array (size=1)
  0 => string '"10064905"' (length=10)    
array (size=3)
  0 => string '"10064788"' (length=10)     
  1 => string '"10064797"' (length=10)     
  2 => string '"10064807"' (length=10)
array (size=1)      
  0 => string '"10063833"' (length=10)       
array (size=1)       
  0 => string '"10063824"' (length=10)      
array (size=2)       
  0 => string '"10063805"' (length=10)      
  1 => string '"10063796"' (length=10)      

preg_match_all()返回二維數組。 默認情況下, $matches[0]包含所有完整的正則表達式匹配項,其余的$matches[n]包含捕獲組n的匹配項。 在您的程序中,您需要遍歷$matches[0]

foreach($matches[0] as $match) {
    $id=$match;
    var_dump($id);
    $sql = mysqli_query($conn, "INSERT into T2 VALUES($id)");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM