Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2012, 11:21 PM   PM User | #1
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
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.
mathceleb is offline   Reply With Quote
Old 08-23-2012, 01:18 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
$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
AndrewGSW is offline   Reply With Quote
Old 08-23-2012, 01:22 AM   PM User | #3
mathceleb
Regular Coder

 
Join Date: Mar 2010
Posts: 235
Thanks: 39
Thanked 6 Times in 6 Posts
mathceleb is an unknown quantity at this point
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.
mathceleb is offline   Reply With Quote
Old 08-23-2012, 12:56 PM   PM User | #4
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
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); 
gvre is offline   Reply With Quote
Users who have thanked gvre for this post:
mathceleb (08-23-2012)
Old 08-23-2012, 12:57 PM   PM User | #5
jaubry
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
jaubry is an unknown quantity at this point
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 ?
jaubry is offline   Reply With Quote
Users who have thanked jaubry for this post:
mathceleb (08-23-2012)
Reply

Bookmarks

Tags
regex, replace

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 AM.


Advertisement
Log in to turn off these ads.