title: Why Sometimes Echoes All Headers While Other Times Not?
body: <p>I am still at beginner level php at procedural style programming.
  Not into php libraries yet. And so want to use pure core plain php to extract words of a page than use DOM and the like.</p>
  <p>I have an html file that looks like the following with headers &amp; paragraphs:</p>
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Title&lt;/title&gt;
   &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;description&quot; content=&quot;Free Web tutorials&quot;&gt;
    &lt;meta name=&quot;keywords&quot; content=&quot;HTML, CSS, JavaScript&quot;&gt;
    &lt;meta name=&quot;author&quot; content=&quot;John Doe&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;article&gt;
  
  
  &lt;h&gt;H: Heading H&lt;/h&gt;
  This is heading 
  &lt;p&gt;P: This is paragraph.&lt;/p&gt;
  
  &lt;h&gt;h2: Heading 2&lt;/h&gt;
  This is heading 2
  &lt;p&gt;p2: This is paragraph 2.&lt;/p&gt;
  
  &lt;h&gt;h: Heading 3&lt;/h&gt;
  This is heading 3
  &lt;p&gt;p: This is paragraph 3.&lt;/p&gt;
  &lt;/article&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>This is very weird!
  Consider the following code ...</p>
  <pre><code>&lt;?php
  
  $url = &quot;http://localhost/Templates/one_page/bot/html_file.php&quot;;
  $html = file_get_contents($url);
  
  
  //ISSUE: Returns only one instance of the header.
  
  function string_between_two_string($html, $starting_word, $ending_word)
  {
      $subtring_start = strpos($html, $starting_word);
      //Adding the strating index of the starting word to 
      //its length would give its ending index
      $subtring_start += strlen($starting_word);  
      //Length of our required sub string
      $size = strpos($html, $ending_word, $subtring_start) - $subtring_start;  
      // Return the substring from the index substring_start of length size 
      return substr($html, $subtring_start, $size);  
  }
    
  $heading = string_between_two_string($html, '&lt;h&gt;', '&lt;/h&gt;'); 
  echo $heading; 
  echo '&lt;br&gt;';
  </code></pre>
  <p>It echoes:
  <strong>H: Heading H</strong></p>
  <p>Now consider this code:</p>
  <pre><code>&lt;?php
  
  $url = &quot;http://localhost/Templates/one_page/bot/html_file.php&quot;;
  $html = file_get_contents($url);
  
  //ISSUE: Returns all instances of the header plus the parapgraphs.
  
  function string_between_two_string($html, $starting_word, $ending_word)
  {
      $subtring_start = strpos($html, $starting_word);
      //Adding the strating index of the starting word to 
      //its length would give its ending index
      $subtring_start += strlen($starting_word);  
      //Length of our required sub string
      $size = strpos($html, $ending_word, $subtring_start) - $subtring_start;  
      // Return the substring from the index substring_start of length size 
      return substr($html, $subtring_start, $size);  
  }
    
  $paragraph = string_between_two_string($html, '&lt;p&gt;', '&lt;/p&gt;'); 
  echo $paragraph; 
  echo '&lt;br&gt;';
  </code></pre>
  <p>It echoes:</p>
  <pre><code>l&gt;
  H: Heading H This is heading
  P: This is paragraph.
  h2: Heading 2 This is heading 2
  p2: This is paragraph 2.
  h: Heading 3 This is heading 3
  p: This is paragraph 3.
  </code></pre>
  <p>Why &quot;l&gt;&quot; gets echoed ?</p>
  <p>NOTE:
  Note both script are 99% same. Only difference is the first one is designed to echo the headers and the second one designed to echo the paragraphs.
  My question is, if the first script only echoes the first header then should not the second script also echo the first paragraph rather than all of the paragraphs ?
  On the other hand, if the second script is echoing fine by echoing all of the paragraphs as it should then should not the first script also echo all the headers (H &amp; h) instead of the first header only ?
  This is puzzling and a mystery!
  What's happening here ?</p>
  <p>Anyway, which script should I work on to have all the headers dumped to an array ($headers=array()) and all the paragraphs dumped to another array ($paragraphs = array()) ? And how to write the lines to achieve this ?</p>
  <p>I really need to know the answer to this mystery!</p>
  <p>Thanks!</p>