php简单的html dom解析器结果未保存

时间:2011-12-07 21:12:24

标签: php domparser

我正在尝试将引用wordpress博客的Youtube的<iframe>标记替换为此[youtube id='THEYOUTUBEID' width='600' height='350']。我使用简单的HTML DOM解析器。虽然我实现了它,但并没有保存到每个帖子中。

任何人都可以帮我理解我错在哪里吗?

$result = mysql_query("SELECT `post_content`  FROM `wp_posts` WHERE `post_content` LIKE '<iframe%' ");

while($row = mysql_fetch_array($result)) {

  $postContent = str_get_html($row["post_content"]);  

   foreach($postContent->find("iframe") as $iframe) {
     $src = $iframe->src;  
     $last = substr( $src, strrpos( $src, '/' )+1 );

     if (stripos($src, "youtube")) {
       $neway = "[youtube id='".$last."' width='600' height='350']";
     } elseif (stripos($src, "vimeo")) {
        $neway = "[vimeo id='".$last."' width='600' height='350']";
     }

     $iframe->outertext = $neway;

     } 
 }

1 个答案:

答案 0 :(得分:0)

似乎你忘了将更改保存到sqldb ..你也忘记了获取表的唯一标识符.. 首先编辑$ result到

$result = mysql_query("SELECT `id`,`post_content`  FROM `wp_posts` WHERE `post_content` LIKE '<iframe%' ");

然后插入此

mysql_query('UPDATE `wp_posts`  SET `post_content` = \''.mysql_real_escape_string($postContent->save()).'\' WHERE `id` = '.$row["id"]);

此后

 $iframe->outertext = $neway;
 }