deleting substring between tokenA, ending in tokenB inclusive w/ regex?

Discussion in 'PHP' started by travelseo, Mar 3, 2011.

  1. #1
    I have some dynamic markup i need to clean up and I think some kind of regex is the way to go. The dynamic part is parameters in an onclick JS call, so I can't use a generic search and replace.


    sample line from markup i am parsing:
    <div class="oddsTableLink" onclick="OpenLink('1236', '140345078', '1');"">
    PHP:
    I want to delete every attribute in this div to just be left with
    <div>    
    PHP:
    can anyone show me how? I found a regex to delete the content BETWEEN my delimiters but not the actual delimiters themselves.

    any help appreciated
    a total regex noob
     
    travelseo, Mar 3, 2011 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    try this:
    
    $str = 'some markup';
    
    $str = preg_replace('/<div([^>]+)>/', '<div>', $str);
    
    Code (markup):
     
    Last edited: Mar 4, 2011
    gapz101, Mar 4, 2011 IP
  3. SpamHat

    SpamHat Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    $html = <<<HTML
    <div class="oddsTableLink" onclick="OpenLink('1236', '140345078', '1');"">';
    HTML
    preg_match('/<div([^>]+)>/si', $html, $m);
    $clean = str_replace($m[1], '', $html);
    ?>
    
    Code (markup):
     
    SpamHat, Mar 4, 2011 IP