1. Halo Guest, pastikan Anda selalu menaati peraturan forum sebelum mengirimkan post atau thread baru.

(Ask) Mengganti image baru di php Regex

Discussion in 'Pemrograman Web' started by amild, Mar 18, 2013.

Tags:
  1. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    halo master php, saya lagi belajar regex di php gan. saya menemukan masalah dalam mengganti tag <img> dalam php


    seperti ini gan, misalnya


    html : Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    <img src="http://example.com/image1.jpg"/>
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    <img src="http://example.com/image2.jpg"/>
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,


    nah saya ingin mengganti gambar baru pada isi html itu "http://example.com/image1.jpg " dan http://example.com/image2.jpg
    menjadi http://image.example.com/image1.jpg dan http://image.example.com/image2.jpg


    kira kira alur logika php beserta penerapannya di php gimana yah mastah ? mohon bantuannya
     
  2. dimasku

    dimasku Super Hero

    Joined:
    Aug 6, 2012
    Messages:
    1,296
    Likes Received:
    151
    Location:
    Surabaya
    ane ga bgitu bs pake regex
    tp klo pake str_replace mestinya mudah

    str_replace("example.com", "image.example.com", $html);


    mungkin yg jago regex bs nambahin
     
  3. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    maksud saya "http://example.com/image1.jpg" dan "http://image.example.com/image1.jpg" gambar yang berbeda
    jadi misalnya gini om , "http://example.com/image1.jpg" adalah file gambar yang belum ter-watermark dan file gambar "http://image.example.com/image1.jpg" sudah ter-watermark. nah nantinya akan berubah seperti om.
     
  4. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    trus nama file "image1.jpg" nya beda antara sebelum dan sesudah?
    Coba kasi contoh string yg real bro, jangan yg hanya sampel karena susah kalo main tebak2an :D
     
  5. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    nama imagenya juga berbeda bro... intinya semua gambar yang ada di dalam halaman tergntikan dengan gambar yang baru dengan gambar yang sudah ter-watermark
     
  6. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    ya nggak bisa donk kalo nama file gambarnya jg beda. ujung2nya pake str_replace biasa :D
    kecuali kasi contoh yg real, baru nanti saya kasi regexpnya :p
     
    kvklay likes this.
  7. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    contoh realnya sudah ada di atas mas... saya baru memikirkan alur logikanya kira2 seperti apa jadinya. ini juga baru belajar mas.
     
  8. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    maksud saya contoh real dengan string yg nyata.
    Karena situ bilang nama file beda antara sebelum & sesudah.
    tapi "di atas" kok pake nama file yg sama, nih contohnya

    Tuh cek, sama2 "image1.jpg" & "image2.jpg" kan.
    Trus contoh kasus yg beda di mana? Mana contoh nama file yg beda. Tulis donk di sini.

    Heran saya, minta dibantu tapi kok nggak mau dibantu ya hehe.. tanya kenapa :D

    Kalo contoh kasusnya aja nggak jelas, gimana orang bisa bantu...
     
    antotok and kvklay like this.
  9. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    oh iya maaf mas.. saya salah tulis..maksud saya alamat url gambar yang berbeda.

    Sekali lagi maaf bukannya saya tidak mau di bantu tapi hanya itu contoh yang bisa saya sampaikan dari tujuan pertanyaan saya.
    kalaupun sudah ada contoh real yang sudah jadi mungkin saya tidak bertanya kesini.
     
  10. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    maksud saya contoh real itu contoh nyata dari kasus agan.
    misal : eh saya punya blog blablabla.com, saya mau replace gambar di sana dari XXX jadi YYYY,
    jadi yang mau bantu jawab bisa cek source html nya.

    bukan contoh kode PHP utk replace yg real :p
     
  11. gudel

    gudel Ads.id Pro

    Joined:
    Dec 13, 2012
    Messages:
    269
    Likes Received:
    24
    TS e d tanya kok mbulet karepe dewe ya? Saya yg nyimak ae ikut bingung. :D
     
  12. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    aduhh mas kok ribet amet sihh minta sampelnya. kalau pakai contoh

    PHP:
     $source "Lorem Ipsum is simply dummy text of the printing and typesetting industry
    <img src='http://example.com/image1.jpg'/> 
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s
    Lorem Ipsum is simply dummy text of the printing and typesetting industry
    <img src='http://example.com/image2.jpg'/> 
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    ;
    itu sudah cukup kok untuk menjabarkan source dan tidak perlu situs aslinya.
     
  13. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    kalo emang bener itu contohnya, pake str_replace / str_ireplace udah bisa.. kenapa harus pake preg_replace? :))
    regexp itu lebih lambat daripada str_replace.. kecuali ada kasus khusus di mana regexp dibutuhkan (di mana saya liat tidak ada alasan utk menggunakan regexp di kasus yg agan kasi) .. :))

    PHP:
    <?php
    $source 
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry
    <img src='http://example.com/image1.jpg'/> 
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s
    Lorem Ipsum is simply dummy text of the printing and typesetting industry
    <img src='http://example.com/image2.jpg'/> 
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    ;

    // Kode ini udah cukup
    $ubah str_ireplace('example.com''image.example.com'$source);

    // Kenapa harus pake preg_replace ???? sia-sia...
    $ubah preg_replace('/example\.com/i''image.example.com'$source);
    ?>
     
    Last edited: Mar 18, 2013
  14. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    yg masi kurang jelas sebener na url image na om :D apa domain tempat image na selalu sama (example.com) ? ato ada image dari domain laen? trus umpama cuma di satu domain, apa struktur folder2/nama file na sama juga, misal example.com/image1.jpg jadi image.example.com/image1.jpg ato beda misal na jadi image.example.com/upload/image1.jpg ato image.example.com/gambarbaru1.jpg :D
     
  15. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar

    benar sekali om domain dan struktur folder2/nama file gambarnya berubah semua.
    misalnya
    gambar sebelum terganti : hxxp://1.bp.blogspot.com/_jmhY7LDr7iw/SSNr7Np3wiI/AAAAAAAAAAU/Ho2XsNmVn00/s1600/earth.sunrise.test.02.jpg
    dan gambar setelah terganti : hxxp://2.bp.blogspot.com/_jmhY7LDr7iw/SSNr7Np3wiI/xxxAxAAAAAU/Ho2XsNmVn00/s1600/earth.sunrise.test.22.jpg
     
  16. khoid

    khoid Super Hero

    Joined:
    Aug 18, 2008
    Messages:
    3,380
    Likes Received:
    256
    Location:
    depok
    klo kaya gitu agak sedikit susah / ribet,, kecuali dikau mau ngelist url yg mo di ganti satu2 :D dan mending pake str_replace kata om xrvel :D kecuali ada kemiripan antara url yg mo diganti sama url pengganti na :D misalnya nama file na sama

    misal gini :
    dikau list dlu url pengganti na, dijadiin array :
    PHP:
    $filebaru = array('hxxp://1.bp.blogspot.com/_jmhY7LDr7iw/SSNr7Np3wiI/AAAAAAAAAAU/Ho2XsNmVn00/s1600/image1.jpg','hxxp://2.bp.blogspot.com/_jmhY7LDr7iw/SSNr7Np3wiI/xxxAxAAAAAU/Ho2XsNmVn00/s1600/image2.jpg');
    content :
    PHP:
    $source "Lorem Ipsum is simply dummy text of the printing and typesetting industry
    <img src='http://example.com/image1.jpg'/> 
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s
    Lorem Ipsum is simply dummy text of the printing and typesetting industry
    <img src='http://example.com/image2.jpg'/> 
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    PHP:
    preg_match_all('!src=["|\'](.*)["|\']!Ums',$source,$a);
    foreach(
    $a[1] as $img){
    $imageName basename($img);
    foreach(
    $filebaru as $imgBaru){
    $imageBaruBame basename($imgBaru);
    if(
    $imageName == $imageBaruName){
    $source=str_replace($img,$imgBaru,$source);
    }}}
    echo 
    $source;
    maap klo ga efesien :peace:
     
    amild likes this.
  17. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    wah mantapppsssss mas khoid. Berhasil !!
    Terima kasih banyak mas O:-)
     
  18. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    coba contoh url gambarnya dikasih dari pertama nanya, pasti mastah xrvel gak bingung :)

    kalau kasus kayak gini untuk gambar dalam jumlah banyak pasti bakal kewalahan bikin list urlnya :D
    apalagi itu folder urlnya dinamis
    mending dibikin domain beda tapi nama filenya sama. dengan kata lain jangan pake picasa dua2nya.
     
  19. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    iya juga sih kalau bikin list url bikin kewalahan :((

    nah saya coba bikin listnya secara otomatis tapi kok gak bisa terganti yah ? masalahnya dimana yah mas?

    PHP:

    $sourceasli 
    ="Lorem Ipsum is simply dummy text of the printing and typesetting industry <a href='http://www.google.com'><img src='http://2.bp.blogspot.com/_SHQ4qVp4jSY/S3TFZk1dUvI/AAAAAAAAACE/gksYwo4z8tQ/s1600/test.jpg'/></a>
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500sLorem Ipsum is simply dummy text of the printing and typesetting industry
    <a href='http://www.google.com'><img src='http://2.bp.blogspot.com/_alcePybThpA/SXbTpQXAhaI/AAAAAAAAAdc/Zo7hCLd0SVg/s1600/testFinalFur1.jpg'/></a>
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    ;  

    $carigambar '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
    preg_match_all($carigambar$sourceasli$hasilPREG_PATTERN_ORDER);$listgambar $hasil[1];
    print_r($listgambar);  

    $source "Lorem Ipsum is simply dummy text of the printing and typesetting industry<img src='http://example.com/image1.jpg'/>
     Lorem Ipsum has been the industry s standard dummy text ever since the 1500sLorem Ipsum is simply dummy text of the printing 
    and typesetting industry<img src='http://example.com/image2.jpg'/> Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    ;  

    preg_match_all('!src=["|\'](.*)["|\']!Ums',$source,$a);
    foreach(
    $a[1] as $img){$imageName basename($img);
    foreach(
    $listgambar as $imgBaru){$imageBaruName basename($imgBaru);
    if(
    $imageName == $imageBaruName){$source=str_replace($img,$imgBaru,$source);
    }}}
    echo 
    $source;  
     
  20. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,873
    Likes Received:
    947
    Location:
    Di sini
    karena kode sebelumnya mewajibkan nama file gambar sama. nah itu di contoh terakhir nggak sama. di $sourceasli "test.jpg", tapi di $source "image1.jpg"
    nah sebenernya mau dibuat wajib sama / nggak?

    kalo mau dibuat tidak wajib sama ya pake ini aja
    PHP:
    <?php
    header
    ('Content-type: text/plain');

    $sourceasli ="Lorem Ipsum is simply dummy text of the printing and typesetting industry <a href='http://www.google.com'><img src='http://2.bp.blogspot.com/_SHQ4qVp4jSY/S3TFZk1dUvI/AAAAAAAAACE/gksYwo4z8tQ/s1600/test.jpg'/></a>
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500sLorem Ipsum is simply dummy text of the printing and typesetting industry
    <a href='http://www.google.com'><img src='http://2.bp.blogspot.com/_alcePybThpA/SXbTpQXAhaI/AAAAAAAAAdc/Zo7hCLd0SVg/s1600/testFinalFur1.jpg'/></a>
    Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    ;  

    $carigambar '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
    preg_match_all($carigambar$sourceasli$hasilPREG_PATTERN_ORDER);$listgambar $hasil[1];

    $source "Lorem Ipsum is simply dummy text of the printing and typesetting industry<img src='http://example.com/image1.jpg'/>
     Lorem Ipsum has been the industry s standard dummy text ever since the 1500sLorem Ipsum is simply dummy text of the printing 
    and typesetting industry<img src='http://example.com/image2.jpg'/> Lorem Ipsum has been the industry s standard dummy text ever since the 1500s" 
    ;  

    preg_match_all('!src=["|\'](.*)["|\']!Ums',$source,$a);

    $source str_replace($a[1], $listgambar$source);
    echo 
    $source;  

    exit();
    ?>
     
    amild likes this.

Share This Page