PDA

View Full Version : error generated... can't explain more


duniyadnd
12-31-2002, 05:56 AM
Frustrated to the core right now, been on this part for over a day now. What I'm trying to do is go through a page and pick up all the sections that might say <b> or </b> and return them to an array. However, I can't even get to go through the document properly.



function findSection($data)
{

preg_match_all("<(/?)B>", $data, $links);

while(list($key,$val) = each($links[2]))
{

if(!empty($val))
$match[] = $val;
//echo $match;

}//end while

while(list($key,$val) = each($links[3]))
{

if(!empty($val))
$match[] = $val;
//echo $match;

}//end while

return $match;
}//end findSection




The error that is constantly generated is this:

Warning: Variable passed to each() is not an array or object in /home/footyman/public_html/mining/thisisit.phtml on line 68

Warning: Variable passed to each() is not an array or object in /home/footyman/public_html/mining/thisisit.phtml on line 77

as an infinite loop. The lines that concern the error are where the while loop lines begin.

Any help would be appreciated.

Thanks
Duniyadnd

eloi_egon
12-31-2002, 09:39 AM
As i can see nothing seems to be wrong with the code!

Dont know if this will help you but give it try. Add the following code to your php-file:

error_reporting(1);

Kiwi
12-31-2002, 10:28 AM
preg_match_all -- according to the documentation -- returns a two dimensional array. $row[0] is the overall pattern split. $row[1] and greater are the matches for each parenthesised expression.

Since you only have one aprenthesised expression, $links will only have two components -- $links[2] and [3] don't exist.

For what you're trying to achieve, you need something like:

$i=(preg_match("/^<[bB]/")?0:1; // if the first character is a bold tag, set $i to 0.
$links=preg_split("/<[bB]>/",
$data); // split the string into lines starting with <b> tags
$n=count($links);
if ($n>1) {
for($i;$n;$i++) {
$a_links=preg_split("/</[bB]>/",
$links[$i]);
/* each row in $links started with a <b> tag
each link in $a_links[0] contains the text before the closing </b> tag
$a_links[1] contains the rest of the string
do whatever you have to do with that text here
this loop will fall over if </b> tags are improperly nested*/
} // for($i;$n;$i++)
else { // if ($n>1)
// no <b> tags found
} if ($n>1)

duniyadnd
12-31-2002, 11:01 PM
actually the original script was okay.

The error i was generating was because i was implementing $data as an array when it should have been a string.

Duniyadnd