$str = '<html><head>
<body><p id="lk"><a href="http://hi.baidu.com">空间</a> <a href="http://baike.baidu.com">百科</a> <a href="http://www.hao123.com">hao123</a><span> | <a href="/more/">更多&gt;&gt;</a></span></p><p id="lm"></p><p><a id="seth" onClick="h(this)" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com" onmousedown="return ns_c({'fm':'behs','tab':'homepage','pos':0})">把百度设为主页</a><a id="setf" onClick="fa(this)" href="javascript:void(0)" onmousedown="return ns_c({'fm':'behs','tab':'favorites','pos':0})">把百度加入收藏夹</a></p>
<p id="lh"><a href="http://e.baidu.com/?refer=888">加入百度推广</a> | <a href="http://top.baidu.com">搜索风云榜</a> | <a href="http://home.baidu.com">关于百度</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">&copy;2012 Baidu <a href="/duty/">使用百度前必读</a> <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a> <img src="http://gimg.baidu.com/img/gs.gif"></p></div></body>
</html>'要求将HTML中的图片提取出来,并对图片base64_encode加密.
用正则和preg_replace行不?本人不会弄,或其它方法也行.谢谢!
 

解决方案 »

  1.   

     <img src="http://gimg.baidu.com/img/gs.gif">這個圖片地址提取出來加密?還是文件提取出來加密?還是?
      

  2.   


    是图片提取并加密.
    base64_encode('http://gimg.baidu.com/img/gs.gif')
      

  3.   


    是的,要HTML中的 JPG GIF 图片并用base64_encode加密后再替换.
      

  4.   

    有個需要注意的地方 str 裏 單引號前面請自己添加\轉義符,可以參考我代碼裏的註釋
    $str = '<html><head>
    <body><p id="lk"><a href="http://hi.baidu.com">空间</a> <a href="http://baike.baidu.com">百科</a> <a href="http://www.hao123.com">hao123</a><span> | <a href="/more/">更多&gt;&gt;</a></span></p><p id="lm"></p><p><a id="seth" onClick="h(this)" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'homepage\',\'pos\':0})">把百度设为主页</a><a id="setf" onClick="fa(this)" href="javascript:void(0)" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'favorites\',\'pos\':0})">把百度加入收藏夹</a></p>
    <p id="lh"><a href="http://e.baidu.com/?refer=888">加入百度推广</a> | <a href="http://top.baidu.com">搜索风云榜</a> | <a href="http://home.baidu.com">关于百度</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">&copy;2012 Baidu <a href="/duty/">使用百度前必读</a> <a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a> <img src="http://gimg.baidu.com/img/gs.gif"></p></div></body>
    </html>';
    // $str=str_replace("'","\'", trim($str));//轉義字符串內單引號字符
    preg_match ( '/img src="([\:a-z\.\/\-\d]+)">/isU', $str, $array );
    $imgurl = isset ( $array [1] ) ? $array [1] : '';if (! empty ( $imgurl )) {
    $imgurl = base64_encode ( $imgurl );
    echo $imgurl; // 輸出encode地址
    } else {
    // 沒有匹配img地址
    }