strip_tags崩溃以获得巨大的价值



我正在使用strip_tags从XML文件中剥离标签,当数组大小较小时它可以正常工作,但如果页面很大,它总是崩溃.这是我的脚本,它适用于多达100个值,但崩溃以获得更大的价值

        preg_match_all("/<image:caption>.*?</image:caption>|<image:loc>.*?</image:loc>|<loc>.*?</loc>/", $str, $results);
         $arr = array_chunk(array_map('strip_tags', $results[0]), 1000);
        for($i=0;$i < 1000;$i++){
      for($j=0;$j < 1000;$j++){

      $output=$arr[$i][$j]. '</br>';

      echo $output;
        }
        }   

它会很好地剥离这些值,但对于更大的文件,它会崩溃。

      <urlset>
        <url><loc>/1366x768/citroen-ds-cabrio-auto-car-wallshark-com-228615.html</loc><image:image><image:loc>s/1366x768/citroen-ds/228615/citroen-ds-cabrio-auto-car-wallshark-com-228615.jpg</image:loc><image:caption>Citroen Ds Cabrio Auto Car Wallshark Com  Walpapers</image:caption></image:image></url>
          <url><loc>/1366x768/citroen-ds-cars-citro-n-cabrio-213157.html</loc><image:image><image:loc>s/1366x768/citroen-ds/213157/citroen-ds-cars-citro-n-cabrio-213157.jpg</image:loc><image:caption>Citroen Ds Cars Citro N Cabrio  Walpapers</image:caption></image:image></url>
          <url><loc>/1366x768/citroen-ds-citro-n-pictures-95569.html</loc><image:image><image:loc>s/1366x768/citroen-ds/95569/citroen-ds-citro-n-pictures-95569.jpg</image:loc><image:caption>Citroen Ds Citro N Pictures  Walpapers</image:caption></image:image></url>
        </urlset>

你可以试试这个:

<pre><?php
$dom = new DOMDocument();
@$dom->load('Remotefile.xml');
$urls = $dom->getElementsByTagName('url');
foreach ($urls as $url) {
    $image = $url->getElementsByTagName('image')->item(0);
    $imageChildren = $image->childNodes;
    $result[] = array( 'loc' => $url->getElementsByTagName('loc')->item(0)->textContent,
                       'imgloc' => $imageChildren->item(0)->textContent,
                       'imgcap' => $imageChildren->item(1)->textContent);
}
$stmt = $dbh->prepare ("INSERT INTO urls (loc, imageloc, imagecap) VALUES (:loc, :imgloc, :imgcap)");
foreach ($result as $res) {
    $stmt -> bindParam(':loc',    $res['loc']);
    $stmt -> bindParam(':imgloc', $res['imgloc']);
    $stmt -> bindParam(':imgcap', $res['imgcap']);
    $stmt -> execute();
}

正则表达式方式:

$pattern = <<<'LOD'
~
  <url>                                                s*+
  <loc>           (?<loc>    [^<]++ ) </loc>           s*+
  <image:image>                                        s*+
  <image:loc>     (?<imgloc> [^<]++ ) </image:loc>     s*+
  <image:caption> (?<imgcap> [^<]++ ) </image:caption> s*+
  </image:image>                                       s*+
  </url>
~x
LOD;
preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);
/* this foreach part is only for cosmetic and is totally useless */
foreach($matches as &$match) {
    foreach($match as $k=>$m) {
        if (is_numeric($k)) unset($match[$k]);
    }
}
print_r($matches);

最新更新