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 <p> 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.<br />";
    //determine position of string <P> in $body
    $strt=strpos($body,"<P>");
    echo "found at ".$strt."<br />";
    //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."<br />";  //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 . "<br />";
}
//this generates the PHP error
foreach($b as $s) {

  echo "Value: " . $s . "<br />";
}
//this outputs the array as it should
?>
</body>
</html>

This code does the following:

  1. Retrieve the BLOB from the database 1 record at a time and assign it to the variable $body.
  2. Strip the slashes.
  3. Count the number of occurrences of <P> in the code always starting from 0. As the While{} runs, it returns the offset for each occurrence of the next <P> as the present occurrence is converted to <p>. Each of these off-set numbers is appended to a string building an array that will be later used.
  4. 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 <p> I can use it for other questionable tags.

Thanks for reading this far. Your input would be appreciated.

D.

echo $a."<br />"; //Note this outputs array(0, 204, 460, 586, 709, 826) in browser as desired and expected

If $a was an array, It wouldn't print anything if you give echo $a. It will give aray().
So, obviously,

foreach($a as $s) {
echo "Value: " . $s . "<br />";
}

that wouldn't work since foreach takes an array as an argument. I don't think $a stores an array.

Are you trying to set variable $a to an array here on line 24:

$a="array(".$strt.", ";
} else {
$a=$a.$strt.", ";

If you are, I don't think this will work, because you have it inside double quotes, I think "array()" is being passed as a string and not being defined as an array. Unless of course you a want to pass it as a string? I may be offbase with this, But I am pretty sure that is it being passed as a string therefore $a is not storing an array.

If $a was an array, It wouldn't print anything if you give echo $a. It will give aray().
So, obviously,
that wouldn't work since foreach takes an array as an argument. I don't think $a stores an array.

Are you trying to set variable $a to an array here on line 24:

$a="array(".$strt.", ";
} else {
$a=$a.$strt.", ";

If you are, I don't think this will work, because you have it inside double quotes, I think "array()" is being passed as a string and not being defined as an array. Unless of course you a want to pass it as a string? I may be offbase with this, But I am pretty sure that is it being passed as a string therefore $a is not storing an array.

That which I am trying to achieve is take the value of variable $strt and append it to a string with each run of the loop.

//if x < 1, then this is the first run of the loop, initiate the string
if ($x<1) {
  $a="array(".$strt.", ";
} else {
//if greater than the first run of the loop, append $strt value to existing string if $a
$a=$a.$strt.", ";
}

Once the loops end the variable $a looks like this:
array(0, 204, 460, 586, 709, 826,

The code

//trim the variable a by 2 (eliminate the last , ) and close off the string
$a = substr($a,0,strlen($a)-2).")";

means this

$a ="array(0, 204, 460, 586, 709, 826".")";

When echo'd to the browser $a looks like 'array(0, 204, 460, 586, 709, 826)'

To me, that looks like $a=array(0, 204, 460, 586, 709, 826). How is that different from simply typing $b=array(0, 204, 460, 586, 709, 826) as I did in my example? The data under $a is not seen as an array but the same string of text under $b is seen as an array.

Since PHP is not interpreting $a as an array, how else can I collect each variable of $strt in a string or some other manner to later declare them as values in an array? If keys are not being stated, does an array always have to be declared by entering $variable = array(value1, value2, value3...)

$a="array(";
$a.="10)";
is different from $a=array("10");

Because in the first case, $a is considered as a string, since "array(" is in double quotes. In the second case, $a is declared as an array. Instead of assigning the value to $a, you can do it this way.

$values="";
for($i=0;$i<10;$i++){
$values.=$i.";";
}

So, by the end of the loop, $values will have a collection of values separated by ;. You can then assign this value to an array by exploding it.
ie., $a=explode(";",$values); So, $a will have an array of values.

This question has already been answered. Start a new discussion instead.