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

[ASK] Code preg_replace untuk menghilang URL dengan kriteria seperti ini.

Discussion in 'Pemrograman Web' started by bosspulsa, May 26, 2013.

  1. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    tapi kalau gak pakai modifier s atau m boss, kalau pake s atau m semacam ini ya new line tetep bablas :)
    Code:
    #<a[^<]*privacy[^<]*</a>#si
    i (PCRE_CASELESS)
    If this modifier is set, letters in the pattern match both upper and lower case letters.

    m (PCRE_MULTILINE)
    By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

    s (PCRE_DOTALL)
    If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier.
    A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

    sumber: hxxp://php.net/manual/en/reference.pcre.pattern.modifiers.php

    yg ini: A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
    kayaknya untuk code ini juga new line akan bablas
    Code:
    #<a[^<]*privacy[^<]*</a>#
    karena ada ^ (negative class) yg selalu match dengan new line character
    CMIIW
     
  2. nnttoo

    nnttoo Super Hero

    Joined:
    Apr 16, 2010
    Messages:
    1,260
    Likes Received:
    620
    Betul new line akan bablas, tapi gak akan bablas ketemu karakter &lt; , stiap tag a kan akan di akhiri dengan &lt;/a jadi psti akan nyangkut di situ.

    Tapi ya seperti saya bilang itu kelemahannya, kalau di tag a juga ada karakter ini juga &lt; maka akan gagal match, meskipun ada kata privacy di dalamnya

    kayaknya gitu sih seperti yg terjadi di kasus agan boss pulsa
     
    Last edited: Jun 17, 2013
  3. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    berarti yg <b>affiliates</b> hrs di str_replace atau str_ireplace dulu ya.
    kalau cuma <b> dan </b> yg direplace, siapa tau di body msh ada n msh dibutuhin.
    tp kl gak dibutuhin ya replace aja semua bold
    Code:
    $string = str_replace(array('<b>', '</b>'), '', $string);
     
  4. nasdin

    nasdin Super Hero

    Joined:
    May 28, 2013
    Messages:
    806
    Likes Received:
    182
    kalau ga mau ribet mikir kemungkinan ini itu pake dom

    PHP:
    $contents file_get_contents('http://www.successful-diet-cabbage-soup.com/quick-weight-loss-plan.html');
    $dom = new DOMDocument();

    @
    $dom->loadHTML($contents);
    $links $dom->getElementsByTagName('a');
    foreach(
    $links as $link) {
        
    $value $dom->saveXml($link);
        if (
    stripos(strip_tags($value), 'affiliates') !== false) {
            
    $contents str_replace($value,'',$contents);
        }
    }

    echo 
    $contents;
     
  5. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    nah ini dia mastahnya udah datang :)
     
  6. nasdin

    nasdin Super Hero

    Joined:
    May 28, 2013
    Messages:
    806
    Likes Received:
    182
    masih newbie ane mah.. :)

    @TS, kalau masih mau pake regex mungkin bisa gini
    PHP:
    $contents file_get_contents('http://www.successful-diet-cabbage-soup.com/quick-weight-loss-plan.html');
    $contents preg_replace('#<a[^>]*>.*?affiliates.*?</a>+#i','',$contents);
    echo 
    $contents;
     
    nnttoo likes this.
  7. nnttoo

    nnttoo Super Hero

    Joined:
    Apr 16, 2010
    Messages:
    1,260
    Likes Received:
    620
    Ini dia gan yang keren,, baru tahu saya kalau di tambahin ? jadi gak babblas sampai yang terakhir,,

    ini hasil testnya biar lebih gampang saya ngetestnya di javascript


    [​IMG]

    lihat garisnya yang pake bintang yang di garis warna merah,,
    hasilnya yang di bawah (di dalam konsol log,, kata aaaaa ikut ke angkut,,

    sedangakan yang warna hijo yang di tambahin tanda ? ama agan nasdin,, bekerja dengan baik artinya dia hanya sampai tag a aja,,

    saya ikutan makasih juga nih,, jadi nambah ilmu,,
     
  8. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    newbidas = nuwbie cerdas :lol:
     
  9. nekaters

    nekaters Hero

    Joined:
    Aug 12, 2012
    Messages:
    673
    Likes Received:
    31
    Location:
    Yogyakarta
    ikut nimbrung ah :D
    mungkin gini jg bisa
    PHP:
    preg_match_all('#<a href="([^"]+)">([^"]+)</a>#'$content$match);
    for(
    $i=0$i<=count($match[2]); $i++) {
     if(
    stripos($match[2][$i], 'privacy') !== false) {
        
    $content str_replace($match[0][$i], ''$content);
     }
    }
     
  10. bosspulsa

    bosspulsa WWW.WARMACHINE.IM

    Joined:
    Jan 27, 2008
    Messages:
    4,036
    Likes Received:
    608
    Location:
    Bantoel, Yogyakarta, Indonesia, Indonesia
    waduh ada respon dari para master ternyata, wah saya coba lagi nanti... makasih banyak ya?
     

Share This Page