Solved

Parse through HTML to find and replace blocks

Posted on 2006-11-28
13
327 Views
Last Modified: 2012-06-27
I have a block of HTML, shown below:

$theWebPage <<< WEBPAGE

<html><head><title>The Page</title></head>
<body>
<h2>A Title</h2>
<img alt="ZCMS|SCRIPTURE|1COL1ROW" src="./scripture.gif" style="left: 507px; position: absolute; top: 85px;" />
<p><strong>Some descriptive text</strong></p>
<img src="this.gif" alt="An image" />
<p>$99.99</p>
<img alt="ZCMS|NEWS|3COL1ROW" src="./news.gif" style="left: 18px; position: absolute; top: 127px;" />
</body>
</html>
WEBPAGE;

I want to parse through the $theWebPage string with PHP and locate all instances of <img> tags that have the special alt="ZCMS|..." substring, and replace it in its entirety with something else. Note, however, that this alt tag can have multiple values, depending on the type of content being replaced, and the style attributes (left and top) will invariably be different for each.  The one constant amongst images to be replaced is the alt="ZCMS| portion...

Thank you!
0
Comment
Question by:godrifle
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 3

Expert Comment

by:jmsloan
ID: 18031386
$newwebpage = str_replace("$oldtext", "$newtext", "$theWebPage");
0
 

Author Comment

by:godrifle
ID: 18031412
Thanks jmsloan. I have no way of knowing precisely what the complete needle string is. Everything in the alt attribute can (and will) vary except the "ZCMS|" portion, and the contents of the style attribute will also change. Therefore, I don't see how using str_replace will work....
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18031638
<?php

$theWebPage = '
<html><head><title>The Page</title></head>
<body>
<h2>A Title</h2>
<img alt="ZCMS|SCRIPTURE|1COL1ROW" src="./scripture.gif" style="left: 507px; position: absolute; top: 85px;" />
<p><strong>Some descriptive text</strong></p>
<img src="this.gif" alt="An image" />
<p>$99.99</p>
<img alt="ZCMS|NEWS|3COL1ROW" src="./news.gif" style="left: 18px; position: absolute; top: 127px;" />
</body>
</html>
';

$replacement = 'Blah';
$theWebPage = preg_replace('/alt="ZCMS/', 'alt="'.$replacement, $theWebPage);
print $theWebPage;

?>
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:godrifle
ID: 18031911
Thanks TeRReF. I think I see where my original question is falling short in describing what I'm looking for.

I want to parse through the $theWebPage string with PHP and locate all instances of <img> tags that have the special alt="ZCMS|..." substring, and replace THE ENTIRE <IMG> TAG STRING with something else.

In a nutshell, I want to search for all that match the initial <img alt="ZCMS| portion, and ignore all image tags that don't have that match...

Most likely this is a regex exercise, which I'm very poor at.

This pseudo-code may explain it better:

foreach(image tag that contains the substring "<img alt="ZCMS|" in $theWebPage){
    perform replacement of the entire image tag
}
0
 
LVL 3

Expert Comment

by:jmsloan
ID: 18031967
Here this may work for you.  Test is out

<?php

function ChangeText($var){

   $text1 = "ZCMS|SCRIPTURE|1COL1ROW";
   $text2 = "ZCMS|NEWS|3COL1ROW";

   $tmpval = substr_count($var, 'ZCMS|');
   if($tmpval > 0){
      for($i=1;$i<=$tmpval;$i++){
         $pos1 = strpos($var, 'ZCMS|');    
         $var2 = substr($var, $pos1);  
         $pos2 = strpos($var2, '"');    
         $str = substr($var, $pos1, $pos2);

         if($str == "$text1"){
            $var = str_replace("$text1", "replace with this1", "$var");
         }else if($str == "$text2"){
            $var = str_replace("$text2", "replace with this2", "$var");
         }
      }
   }

return $var;

}

$html = '<html><head><title>The Page</title></head>
         <body>
         <h2>A Title</h2>
         <img alt="ZCMS|SCRIPTURE|1COL1ROW" src="./scripture.gif" style="left: 507px; position:
            absolute; top: 85px;" />
         <p><strong>Some descriptive text</strong></p>
         <img src="this.gif" alt="An image" />
         <p>$99.99</p>
         <img alt="ZCMS|NEWS|3COL1ROW" src="./news.gif" style="left: 18px; position: absolute; top: 127px;" />
         </body>
         </html>';

$newhtml = ChangeText("$html");

echo "$newhtml";

?>
0
 

Author Comment

by:godrifle
ID: 18032139
Thanks jmsloan and others. I suspect I posted my last comment while you were composing your reply. Please check it out as it sheds better light on why your solution doesn't work.

Once the darn <img> tags are caught, I intend to parse through each one and take actions based upon position and the other values inside the alt attribute. I'm nearly positive this is a regex exercise, but I don't know how to cope with that! ;-)
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 18035278
<?php
$theWebPage = <<< WEBPAGE
<html><head><title>The Page</title></head>
<body>
<h2>A Title</h2>
<img alt="ZCMS|SCRIPTURE|1COL1ROW" src="./scripture.gif" style="left: 507px; position: absolute; top: 85px;" />
<p><strong>Some descriptive text</strong></p>
<img src="this.gif" alt="An image" />
<p>$99.99</p>
<img alt="ZCMS|NEWS|3COL1ROW" src="./news.gif" style="left: 18px; position: absolute; top: 127px;" />
</body>
</html>
WEBPAGE;
$theWebPage = preg_replace_callback('/([\w]+)\\|([\w]+)\\|([\w\d]+)/', 'changeAlt', $theWebPage);
echo $theWebPage;

function changeAlt($key){
      //mysql_query("select * from table where field1='{$key[1]}' and field2='{$key[2]}' field3='{$key[3]}'");
      // other method;
      
      //$array[$key[1]][$key[2]][$key[3]];
      $array=array();
      $array["ZCMS"]["SCRIPTURE"]["1COL1ROW"]="data1";
      $array["ZCMS"]["NEWS"]["3COL1ROW"]="data2";
      $data=$array[$key[1]][$key[2]][$key[3]];
      
      return $data;
      }
?>
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18035331
How about this, it looks for image tag with the ZCMS string and stores them in the $matches array. It will also store the offset, so you know where the match occured in the string. Now you can use the $matches array to replace them to whatever you want...

<?php

$theWebPage = '
<html><head><title>The Page</title></head>
<body>
<h2>A Title</h2>
<img alt="ZCMS|SCRIPTURE|1COL1ROW" src="./scripture.gif" style="left: 507px; position: absolute; top: 85px;" />
<p><strong>Some descriptive text</strong></p>
<img src="this.gif" alt="An image" />
<p>$99.99</p>
<img alt="ZCMS|NEWS|3COL1ROW" src="./news.gif" style="left: 18px; position: absolute; top: 127px;" />
</body>
</html>
';

preg_match_all('/<img.*?alt="ZCMS[^>]+>/', $theWebPage, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

?>
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 60 total points
ID: 18035402
Of course, if you want to replace all matches with the same value, you can just do this:

<?php

$theWebPage = '
<html><head><title>The Page</title></head>
<body>
<h2>A Title</h2>
<img alt="ZCMS|SCRIPTURE|1COL1ROW" src="./scripture.gif" style="left: 507px; position: absolute; top: 85px;" />
<p><strong>Some descriptive text</strong></p>
<img src="this.gif" alt="An image" />
<p>$99.99</p>
<img alt="ZCMS|NEWS|3COL1ROW" src="./news.gif" style="left: 18px; position: absolute; top: 127px;" />
</body>
</html>
';

$replacement = 'blah';
$theWebPage = preg_replace('/<img.*?alt="ZCMS[^>]+>/', $replacement, $theWebPage);
print($theWebPage);

?>
0
 

Author Comment

by:godrifle
ID: 18037109
TeRReF, You're right on it. Thank you. One last question before I reward you the points. Do you see any flaws in the following pattern to match the entire style="..." string:

preg_match('/style=".*?[^"]+"/'

Thanks so very much for pointing me in the right direction!
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18037571
This is better:
preg_match('/style="[^"]+"/'

since this will already match any character that's not a "
[^"]+
0
 

Author Comment

by:godrifle
ID: 18039001
Thank you TeRReF. You've been a great help! And thank you to everyone else who commented.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18039061
You're welcome :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question