How to preg_replace some ID's inside my PHP code?

Discussion in 'PHP' started by nasi, Jan 5, 2012.

  1. #1
    Hi,
    I have a script that import content into my website,and I want to replace some data.I can replace div classes,but not the ID's inside the classes.Example:

    <div class="Content" id="i_need to remove that" data-shortlistID="I have to rename" data-natID="remove agian">

    So,to remove the div class i use:

    $html = preg_replace('#<div[^>]*class="Content"[^>]*>.*?</div>#is', '', $html);

    To rename the div class i use:

    $html = preg_replace('#(<div[^>]*class=")Content("[^>]*>.*?</div>)#is', '$1New-Content$2', $html);

    But how can remove/rename the ID's inside <div class="Content" ?

    I'm not coder,so please excuse me poor language:)
    Thanks in advance
    Krasi
     
    nasi, Jan 5, 2012 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    $html = preg_replace('#class="Content" id="(.*?)"#','',$html);  // replace with nothing or insert class="Content" between the empty '' quotes
    $html = preg_replace('#data-natID="(.*?)"#','',$html);  // replace with nothing
    
    
    $somethingelse = "something";  // new data-shortlistID
    
    $html = preg_replace('#data-shortlistID="(.*?)"#','data-shortlistID="'.$somethingelse.'"',$html);  // renames data-shortlistID with $somethingelse
    PHP:
     
    Last edited: Jan 6, 2012
    MyVodaFone, Jan 6, 2012 IP
  3. nasi

    nasi Active Member

    Messages:
    135
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Thanks very much MyVodaFone!!You are great person!!!
     
    nasi, Jan 6, 2012 IP