2011-12-15 11 views
1

は、私は、コード例を持っている:phpのエコーリンクURL配列を修正する方法は?

include 'simple_html_dom.php'; 
    // convert link url 
    function foo($uri) { 
     $url = parse_url($uri); 
     $paths = explode('/', $url['path']); 
     return sprintf("%s://%s/%s", $url['scheme'], 'localhost/test/get_image/images', end($paths)); 
    } 
    $str = '<img src="http://www.somedomain.com/somepic.jpg" /> 
    <img src="http://www.microsoft.com/somepic.jpg" />'; 
    $html = new simple_html_dom(); 
    $html->load($str); 
    // remove all image 
    $arr_img = array(); 
    foreach($html->find('img') as $element) { 
     $arr_img[] = $element->src; 
    $str_rep = str_replace($element->src, foo($element->src), $str); 
    } 
    echo $str_rep; 

OUTPUT:リンク "http://www.somedomain.com/somepic.jpg" を取得することはできません

<img src="http://www.somedomain.com/somepic.jpg"> 
<img src="http://localhost/test/get_image/images/somepic.jpg"> 

エラー変換しない「のhttp://localhost/test/get_image/images/somepic.jpg」 それを修正する方法は?

答えて

0

あなたのforeachループで$str_repに何かを割り当てるが、それ以外で変数を使用している:あなただけの最後の試合を処理します。

代わりにこのような何かを試してみてください:

foreach($html->find('img') as $element) { 
    $arr_img[] = $element->src; 
    $str_rep = str_replace($element->src, foo($element->src), $str); 
    echo $str_rep; 
} 
+0

結果は次のとおりです。テスト

関連する問題