php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14220 break doesn't stop foreach from iterating over array
Submitted: 2001-11-25 23:18 UTC Modified: 2001-11-26 02:59 UTC
From: tim dot lokot at s1 dot com dot au Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.0.6 OS: Win2K
Private report: No CVE-ID: None
 [2001-11-25 23:18 UTC] tim dot lokot at s1 dot com dot au
I'm running the following function in conjunction with the DOM XML stuff to create a SAX parser.  Trouble is that the following code doesn't seem to stop iterating over the $startTags array when it hits the break command.

function startElement ($parser, $name, $attrs)
{
  global $startTags, $xmlPath, $htmlContent, $monthWords;

  $xmlPath .= "/$name";

  foreach ($startTags as $tag => $value)
  {
    if (substr($xmlPath, strlen($xmlPath) - strlen($tag), strlen($tag)) == $tag)
    {
      $htmlContent .= $startTags[$tag];

      if ($name == "url")
      {
        if (isset ($attrs["target"]))
        {
	  $htmlContent .= " target=\"$attrs[target]\"";
	}
	$htmlContent .= " href=\"$attrs[page]\">";
	break;	// Stop iterating through tags
      }
      else if ($tag == "events/date")
      {
	$htmlContent .= "<div class=\"hdr2\">" . $monthWords[$attrs["month"]] . ", $attrs[year]</div></div>\n<div class=\"content\">";
      }
    }
  }
}

An exmaple array for $startTags is as follows:

$startTags = array (
	"content/section" => "<div class=\"content\">",
	"text" => "<div class=\"txt2\">",
	"list" => "<ul class=\"listtext\">",
	"list/item" => "<li>",
        "item" => "junk"
);

If you were trying to match up the item tag, then the loop would actually print out the value of list/item and item, instead of breaking after it found list/item.

This also assumes that the xml for this match is like this
<list>
<item>point 1</item>
<item>point 2</item>
</list>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-25 23:34 UTC] tim dot lokot at s1 dot com dot au
Code was goofy .... only been looking at for a few days wondering what was going on
 [2001-11-26 02:59 UTC] mfischer@php.net
We rather call it Bogus than Goofy ;)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 30 22:01:28 2024 UTC