Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    Regex Replacement within an html tag

    I want to convert div tags to anchor tags below
    PHP Code:
    $html '<div class="PopActivateApply" style="background-color: #e0d2bc;">
                                        <img class="PopCloseApply" src="/sites/all/themes/tester/images/universities/pop_up_close.png" alt="Close" id="PopUpCloseApply">blahblahblah</div>'
    ;

    $div_classes = array('PopActivateApply');
    // replace div tags to anchor tag
    foreach ($div_classes as $dc){
       
    $html =  preg_replace('/(\<)(div)( class\=\")(' $dc ')(.*)(\<\/)(div)(\>)/''$1a$3$4$5$6a$8'$html);

    It's still showing it as a div tag afterwards.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    $div_classes = array('PopActivateApply');
    this creates an array with a single string element - the word 'PopActivateApply' - it has no connection to the variable $html

    If you just want to replace 'div' with 'a' then I'm not sure why you need regex:

    PHP Code:
    $html1 str_replace('<div''<a'$html);
    $html_final str_replace('div>''a>'$html1); 
    Regex might be useful if the html is malformed; that is, if there are unwanted spaces in the original.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by AndrewGSW View Post
    this creates an array with a single string element - the word 'PopActivateApply' - it has no connection to the variable $html

    If you just want to replace 'div' with 'a' then I'm not sure why you need regex:

    PHP Code:
    $html1 str_replace('<div''<a'$html);
    $html_final str_replace('div>''a>'$html1); 
    Regex might be useful if the html is malformed; that is, if there are unwanted spaces in the original.
    I'm doing the array because we eventually want to use other div classes in there. I cannot use the code you posted above because we need to target certain div classes inside a large html string, not all div classes, but certain ones in the array only. I did not post the entire string so that things would not get garbled.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    239
    Thanks
    1
    Thanked 56 Times in 55 Posts
    PHP Code:
    $html '<div class="PopActivateApply" style="background-color: #e0d2bc;"><img class="PopCloseApply" src="/sites/all/themes/tester/images/universities/pop_up_close.png" alt="Close" id="PopUpCloseApply">blahblahblah</div><div class="foo">foo</div>';

    $div_classes = array('PopActivateApply''foo');
    $replace_classes implode("|"$div_classes);

    $pattern '#<div(.+?)class="(' $replace_classes ')"(.*?)>([^<]+)</div>#si';

    $html preg_replace($pattern'<a\1class="\2" \3>\4</a>'$html); 

  • Users who have thanked gvre for this post:

    mathceleb (08-23-2012)

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hello,
    I've tryed :
    PHP Code:
    $html =  preg_replace('/(\<div)( class\=\")(' $dc ')([^\>]*\>)([^\<]*)(.*)(\<\/div\>)/''<a$2$3$4$5$6</a>'$html); 
    And it works for me.

    Can you try ?

  • Users who have thanked jaubry for this post:

    mathceleb (08-23-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •