I'm a newbie to PHP and arrays are giving me a problem.
I'm converting BLOBs of data from a MySQL database. Each of these BLOBs is the body portion of HTML files I'm converting to XHTML. Mainly, I'm checking tags for case and closing tag. There are over 500 of these BLOBS. Hence, I want the automate the checking and editing of tags as much as possible.
Here is the code I have written so far, abbreviated to the section I am having problems with and what I want to achieve. Explanation appears below.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
... Code ...
$i=0;
while ($i < 1) {
$body=mysql_result($result,$i,"body");
$body=stripslashes($body);
$i++;
}
//number of occurrences of <P> in $body as a variable; While {} will repeat
//this many times
$i=substr_count($body,"<P>",0);
//initialise counter variable for While {}
$x=0;
while ($x < $i){
echo "<P> occurs ".substr_count($body,"<P>",0)." times.";
//determine position of string <P> in $body
$strt=strpos($body,"<P>");
echo "found at ".$strt."";
//if first time through this While {}, initialise the variable
//ELSE build on it, separating values with ,
if ($x<1) {
$a="array(".$strt.", ";
} else {
$a=$a.$strt.", ";
}
//redefine body by replacing <P> with <p> at off-set in $strt
$body=substr_replace($body,"<p>",$strt,3);
$x++;
}
//trim the variable a by 2 (eliminate the last , ) and close off the string
$a = substr($a,0,strlen($a)-2).")";
echo $a.""; //Note this outputs array(0, 204, 460, 586, 709, 826) in browser as desired and expected
$b = array(0, 204, 460, 586, 709, 826);
//for testing what could be the problem, I copied the array as echo'd to the browser and assigned it here to $b
foreach($a as $s) {
echo "Value: " . $s . "";
}
//this generates the PHP error
foreach($b as $s) {
echo "Value: " . $s . "";
}
//this outputs the array as it should
?>
</body>
</html>
This code does the following:Retrieve the BLOB from the database 1 record at a time and assign it to the variable $body.
Strip the slashes.
Count the number of occurrences of in the code always starting from 0. As the While{} runs, it returns the offset for each occurrence of the next
as the present occurrence is converted to
. Each of these off-set numbers is appended to a string building an array that will be later used.
The lines of code with echo at the start are merely for tracking progress in this debugging phase.
The output of what this achieves looks like this:
occurs 6 times.
found at 0
occurs 5 times.
found at 204
occurs 4 times.
found at 460
occurs 3 times.
found at 586
occurs 2 times.
found at 709
occurs 1 times.
found at 826
array(0, 204, 460, 586, 709, 826) <-- output of line 34 in code above
Warning: Invalid argument supplied for foreach() in D:\...\parse.php on line 86 [N.B.: line 36 of code posted above]
<-- below is the result of the Foreach on lines 42-45 above.
Value: 0
Value: 204
Value: 460
Value: 586
Value: 709
Value: 826
To me, there is no difference between $b and $a, yet for some reason PHP does not like the string in $a and generates the error of an invalid argument. Any ideas how to make this work?
The purpose of all this is so that I have reference points in the BLOB to work on each occurrence of a particular HTML tag. If I can get this to work for I can use it for other questionable tags.
Thanks for reading this far. Your input would be appreciated.
D.