[SOLVED] [PHP] Menemukan sebuah kata dan merubahnya

Discussion in 'Programming' started by Fahmi, May 16, 2014.

  1. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Pada beberapa ujicoba, pertama saya ingin membuat dalam 1 page(halaman) jika menemuka sebuah kata "Type" maka akan otomatis berubah menjadi sebuah link :
    Code:
    <a href="http://test-url.com/test-url">Type</a>
    Penasaran mencoba dengan kode PHP tetap gk berhasil :( , padahal enaknya memakai client side.. tapi saya coba akalin dengan sebaris kode jquery ternyata bisa, berikut kode nya:

    Anggap 1 buah konten berada pada ID = #contoh-id
    Code:
    $(document).ready(function() {
      $("#contoh-id").html($("#contoh-id").html().replace(/Type/g, '<a href="http://test-url.com/test-url">Type</a>'));
    });  
    Nah kalau boleh dong, misal ada yg bisa -- contohin kalau ini bisa dibuat menggunakan kode PHP :)

    Update - Result :

    Anggap sebuah isi dari konten adalah $content

    1. Preg_Replace :

    Code:
    $findtext = array();
    $findtext[1] = '/Type/';
    $replace = array();
    $replace[1] = '<a href="http://test-url.com/test-url">Type</a>';
             
    $result = preg_replace($findtext, $replace, $content);

    2. Str_Replace :

    Code:
    $findtext = array("Type", "Sound");
    $replace = array("<a href="http://test-url.com/test-url">Type</a>", "<a href="http://test-url.com/test-url">Sound</a>");
    
    $result = str_replace($findtext, $replace, $content);
     
    Last edited: May 21, 2014
  2. wphoet

    wphoet You'll Never Walk Alone

    Joined:
    Feb 19, 2013
    Messages:
    1,149
    Likes Received:
    142
    Trophy Points:
    63
    Google+:
    hmmmm....*bergaya*coba tunggu yang dibawah sya siapa tau bisa *ngupil*hehehe...*ketawa4**mati*
     
  3. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Koq nunggu sih? *berdarah* okeh deh sipp *bagus*
     
  4. Ardilas

    Ardilas Super Level

    Joined:
    Feb 18, 2013
    Messages:
    4,243
    Likes Received:
    317
    Trophy Points:
    83
    Google+:
    Kurang jago php nih. Tapi menurut saya tidak sesederhana Jquery, logikanya tiap kali artikel / tulisan sebelum di munculin harus di baca dulu. Jika terdapat tulisan "type" maka diberi link.
     
  5. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Iya bener begitu dig, kalau di Jquery kan sesudah halaman tampil baru di cari katanya, nah lebih bagus menggunakan PHP, jadi sebelum 1 page tampil.. kode sudah mencari kata tsb dan merubahnya menjadi link misalnya .. :D
     
  6. Dan

    Dan Forum Bot

    Joined:
    Dec 4, 2012
    Messages:
    1,316
    Likes Received:
    257
    Trophy Points:
    83
    Google+:
    droidplus and Fahmi like this.
  7. Ardilas

    Ardilas Super Level

    Joined:
    Feb 18, 2013
    Messages:
    4,243
    Likes Received:
    317
    Trophy Points:
    83
    Google+:
    Mohon coba ini Den @Fahmi,
    Code:
    $text ="Hello type hello";
    $link ="<a href="http://test-url.com/test-url">type</a>";
    $newtext = str_replace("type", $link, $text);
     
    Fahmi likes this.
  8. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Saya coba cara dari Mas Dan ini : berikut contoh nya :

    Code:
                $patterns = array();
                $patterns[0] = '/Type/';
                $patterns[1] = '/Sound/';
                $replacements = array();
                $replacements[0] = '<a href="http://test-url.com/test-url">Type</a>';
                $replacements[1] = '<a href="http://test-url2.com/test-url">Sound</a>';
                $this->data['description'] = preg_replace($patterns, $replacements, html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'));
    Dan Berhasil Yesss !

    Dan cara dari Bro Ardillas ini :
    Code:
    $type = '<a href="http://test-url.com/test-url">Type</a>';
    $this->data['description'] = str_replace('Type', $type, html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'));
    Berhasil juga Yess, tapi kendala cuman bisa merubah satu kali saja, kesimpulan :

    Saya menggunakan preg_replace saja berarti supaya bisa banyak merubahnya :D
     
  9. Dan

    Dan Forum Bot

    Joined:
    Dec 4, 2012
    Messages:
    1,316
    Likes Received:
    257
    Trophy Points:
    83
    Google+:
    Ingat semua sudah ada panduannya, tinggal mau pilih yang mana yang cukup membingungkan, mana yang perfomanya baik dll. :)
     
  10. Ardilas

    Ardilas Super Level

    Joined:
    Feb 18, 2013
    Messages:
    4,243
    Likes Received:
    317
    Trophy Points:
    83
    Google+:
    Saya cuma ikut mendiskusikan saja. Hehehe.
     
  11. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Performa preg_replace bagaimana mas Dan ? :D saya pakai untuk merubah 5 kata saja sih :D yaa kedepan pasti akan ada yg dirubah lagi sih hehe
     
  12. Dan

    Dan Forum Bot

    Joined:
    Dec 4, 2012
    Messages:
    1,316
    Likes Received:
    257
    Trophy Points:
    83
    Google+:
    Paling umum digunakan jadi perfoma cepat :)
     
  13. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Oh iya str_replace pakai Array bisa juga kali mas @Dan yah?
     
  14. ncang

    ncang Super Level

    Joined:
    Feb 7, 2013
    Messages:
    4,653
    Likes Received:
    761
    Trophy Points:
    113
    Google+:
    hanya berlaku untuk kata yah ? bukan kalimat :D keren nih kasian juga kan kalau manual *jail*
     
  15. Dan

    Dan Forum Bot

    Joined:
    Dec 4, 2012
    Messages:
    1,316
    Likes Received:
    257
    Trophy Points:
    83
    Google+:
  16. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
  17. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Saya coba update siapapun yang mau pakai :D

    Anggap sebuah isi dari konten adalah $content

    1. Preg_Replace :

    Code:
    $findtext = array();
    $findtext[1] = '/Type/';
    $replace = array();
    $replace[1] = '<a href="http://test-url.com/test-url">Type</a>';
              
    $result = preg_replace($findtext, $replace, $content);

    2. Str_Replace :

    Code:
    $findtext = array("Type", "Sound");
    $replace = array("<a href="http://test-url.com/test-url">Type</a>", "<a href="http://test-url.com/test-url">Sound</a>");
    
    $result = str_replace($findtext, $replace, $content);
    Apakah benar Array str_replace seperti diatas mas @Dan ?

    Nah semoga paham dan berguna :D
     
  18. Ardilas

    Ardilas Super Level

    Joined:
    Feb 18, 2013
    Messages:
    4,243
    Likes Received:
    317
    Trophy Points:
    83
    Google+:
  19. Fahmi

    Fahmi Newbie

    Joined:
    Dec 5, 2012
    Messages:
    1,719
    Likes Received:
    159
    Trophy Points:
    63
    Google+:
    Berkat pertolongan dari Den @Dan dan @Ardilas juga kan *yess*
     
    Ardilas likes this.
  20. aku sukses

    aku sukses Member

    Joined:
    Jul 21, 2014
    Messages:
    197
    Likes Received:
    11
    Trophy Points:
    18
    Google+:
    gunakan saja array kata-kata yang mau diubah jadi link, kalau pengguna wordpress bisa menggunakan plugin auto link
     
Loading...
Similar Threads - [SOLVED] [PHP] Menemukan
  1. apartemen
    Replies:
    11
    Views:
    5,905

Share This Page