Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT!! How to Subsitute string in HTML?

Status
Not open for further replies.

fyyap

Programmer
Oct 29, 2001
9
SG
Hi,

I have a problem in how can I subsitute the html tag:

<img alt=&quot;&quot; border=&quot;0&quot; src=&quot;/banner_goodlife2.swf&quot;>

with

<object classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot; codebase=&quot; width=&quot;140&quot; height=&quot;140&quot;>
<param name=movie value=&quot;/banner_goodlife2.swf&quot;>
<param name=quality value=high>
<embed src=&quot;/banner_goodlife2.swf&quot; quality=high pluginspage=&quot; type=&quot;application/x-shockwave-flash&quot; width=&quot;140&quot; height=&quot;140&quot;> </embed> </object>

whereby the file name is dynamic?In this case the file name &quot;/banner_goodlife2.swf&quot;.

this is my regular expression:

$detail =~ s/<img(.*).swf&quot;>//g
 
Code:
my $pattern = '<img.*\.swf&quot;>';
my $replace = qq~
<object classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot; codebase=&quot;[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0&quot;;[/URL] width=&quot;140&quot; height=&quot;140&quot;>
<param name=movie value=&quot;/banner_goodlife2.swf&quot;>
<param name=quality value=high>
<embed src=&quot;/banner_goodlife2.swf&quot; quality=high 
pluginspage=&quot;[URL unfurl="true"]http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash&quot;;[/URL] type=&quot;application/x-shockwave-flash&quot; width=&quot;140&quot; height=&quot;140&quot;></embed></object>
~;
$detail =~ s/$pattern/$replace/g;

A little messy, but it works...And I'm certain there's a safer regex than the one I used...[wink]

I'm not sure what you mean by the dynamic file. Do you mean each <img> tag will have a different src..?

HTH. Notorious P.I.G.
 
Hi PerlIsGood,

Thanks for your quick reply. Yup, u r rite. The image source is always different. And there might be a few <img> tag in the content.

thanks.
 
Ahh, makes it a little more difficult. Regex's aren't my strong point, but you'll need to change it to look something like this (experts feel free to improve on pattern [smile]):

Code:
@my html_file;
$html_file[0] = qq~<img alt=&quot;&quot; border=&quot;0&quot; src=&quot;/banner_goodlife2.swf&quot;>~;
$html_file[1] = qq~<img alt=&quot;&quot; border=&quot;0&quot; src=&quot;/file1.jpg&quot;>~;
$html_file[2] = qq~<img alt=&quot;&quot; border=&quot;0&quot; src=&quot;/file2.swf&quot;>~;

foreach ( @html_file ) {
  if ( /<img.+?[^<>]src=&quot;(.+?\.swf)&quot;>/ ) {
  print &quot;\n($1)\n&quot;; #debug
  $replace = qq~
<object   classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot; codebase=&quot;[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0&quot;;[/URL] width=&quot;140&quot; height=&quot;140&quot;><param name=movie value=&quot;$1&quot;><param name=quality value=high><embed src=&quot;$1&quot; quality=high pluginspage=&quot;[URL unfurl="true"]http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Vers[/URL]  ion=ShockwaveFlash&quot;;type=&quot;application/x-shockwave-flash&quot; width=&quot;140&quot;   height=&quot;140&quot;></embed></object>
~;
  s/<img.+?[^<>]src=&quot;(.+?\.swf)&quot;>/$replace/g;
  print &quot;$_\n&quot;; #debug
  }
}

Successfully skips $html_file[1] entry, but it only works for this example. If reading an entire line from an html file, that pattern would still grab multiple <img> tags - i.e., it will grab everything in the following parentheses and replace it:

Code:
<html><body>
(<img alt=&quot;&quot; src=&quot;title.jpg&quot;>
<a href=&quot;blah.txt&quot;>
Other random text/tags until it finds
an img tag with .swf in it...
<img alt=&quot;&quot; src=&quot;/banner.swf&quot;>)

html is tricky business...sry i can't help more. Notorious P.I.G.
 
Aha! Forget the regex, just read in another thread of the HTML::parser module. I hear it makes html tag matching much easier once ya get the hang of it.

You can check the documentation here:

/thanx to jaa for posting that in the other thread, and consequently cluing me in to that module/ [peace] Notorious P.I.G.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top