【问题标题】:php simple html dom parser result is not savedphp简单的html dom解析器结果未保存
【发布时间】:2011-12-07 21:12:23
【问题描述】:

我正在尝试将 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;

     } 
 }

【问题讨论】:

  • 你在问为什么它没有保存到数据库中?因为您的代码中没有查询将新信息发送回上游到数据库,除非您的 $iframe 对象在您设置其 outertext 属性时自动处理...
  • 不,iframe 对象不这样做。我认为它确实是愚蠢的。现在我知道我必须有一个更新查询才能保存它......

标签: php domparser


【解决方案1】:

您似乎忘记将更改保存到 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;
 }

【讨论】:

    猜你喜欢
    • 2014-01-15
    • 2011-08-27
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多