title: Need help using PHP to replace text strings with the contents of a set of external files
body: <p>I am working on a project to improve public accessibility to the inner workings of a legislative body and I've hit a roadblock. Hoping to get some help since I've been searching for hours and getting nowhere.</p>
  
  <p>Basically, I have some data I want to manipulate. The source currently looks like this:</p>
  
  <blockquote>
    <p>HB 2434 HB 1980 SB 5234 SB 6185 HB 1320 SB 5238 HB 2239 HB 2224 HB 1052 HB 1032 SB 6178 SB 6185 HB 1320</p>
  </blockquote>
  
  <p>On another day it might look like this:</p>
  
  <blockquote>
    <p>SB 5234 SB 6185 HB 1320 SB 6178 SB 6185 SB 5238 HB 2239 HB 2224 HB 1980 HB 1032 HB 1320 HB 1052 HB 2434</p>
  </blockquote>
  
  <p>Each of these (i.e. HB 2434 or SB 5324) is a bill number, referring to a piece of legislation. The order is important - these bills are listed by date modified, with the most recently modified bill first. The order changes on a regular basis as the file is regenerated. The format doesn't change; it's always just a text file with a list of bills separated by a space. </p>
  
  <p>I want to replace each of the bill numbers listed above with content housed in external files, and then include that content on a webpage. I have a set of external files with explanatory info corresponding to each bill number (i.e. SB5324.html). </p>
  
  <p>So "SB 5324" would be replaced with the contents of SB5324.html, which resemble something like this:</p>
  
  <pre><code>&lt;div&gt;
    &lt;h2&gt;HB 5324 Information&lt;h2&gt;
    &lt;p&gt;John Doe is the primary sponsor of this bill. The bill was introduced on January 1st, 2013 by Reps. Doe of Cooltown and Jane Smith of Anytown. It is scheduled for a hearing in the Committee that Doesn't Matter on March 18th, 2013. Comments regarding the bill may be directed to Rep. Doe.&lt;/p&gt; &lt;p&gt;Recent comments about the bill:&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;First comment&lt;/li&gt;&lt;li&gt;Second comment&lt;/li&gt;&lt;li&gt;Third comment&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Each of the other bills would be replaced with something similar.</p>
  
  <p>The order is important to the end result because I want the divs that appear at top of the page to correspond to the bills with the recent activity, and inactive bill divs to be at the bottom. </p>
  
  <p>What is the best way to go about this with PHP and cURL? I understand basic usage of cURL but I'd like to include the source file and then replace each bill number in order with a small file consisting of nothing but content wrapped in a div like the above. The files are all stored in the same place, and accessible like this:</p>
  
  <p><a href="http://website.tld/bills/divs/SB5324.html" rel="nofollow">http://website.tld/bills/divs/SB5324.html</a>
  and
  <a href="http://website.tld/bills/divs/HB1980.html" rel="nofollow">http://website.tld/bills/divs/HB1980.html</a></p>
  
  <p>I've been trying to get this to work with just two of the bills, but I'm sure I'm going about this wrong:</p>
  
  <pre><code>&lt;?php function getBills($billlistid) {$ch = curl_init(); $timeout = 5;
  curl_setopt ($ch, CURLOPT_URL, 'http://website.tld/bills/' .
  $billlistid . '.html'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents =
  curl_exec($ch); if (curl_errno($ch)) {echo "&lt;p&gt;Sorry, can't show the bills! Try refreshing the page.&lt;/p&gt;"; } else {
  curl_close($ch);
  $file_contents = preg_replace('/SB 5324/', file_get_contents($_SERVER['DOCUMENT_ROOT'].'/bills/divs/SB5324.html'), $file_contents);
  $file_contents = preg_replace('/HB 1980/', file_get_contents($_SERVER['DOCUMENT_ROOT'].'/bills/divs/HB1980.html'), $file_contents);
  echo $file_contents; }}?&gt;
  
  &lt;?php getBills('MyBillList.txt') ?&gt;
  </code></pre>
  
  <p>I'm new to PHP so would appreciate some pointers. Thanks. </p>
  
  <p><strong>POSTSCRIPT</strong>: The PHP code I have now is producing this:</p>
  
  <pre><code>&lt;div&gt;
    &lt;h2&gt;SB 5234 Information&lt;h2&gt;
    &lt;p&gt;John Doe is the primary sponsor of this bill. The bill was introduced on January 1st, 2013 by Reps. Doe of Cooltown and Jane Smith of Anytown. It is scheduled for a hearing in the Committee that Doesn't Matter on March 18th, 2013. Comments regarding the bill may be directed to Rep. Doe.&lt;/p&gt; &lt;p&gt;Recent comments about the bill:&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;First comment&lt;/li&gt;&lt;li&gt;Second comment&lt;/li&gt;&lt;li&gt;Third comment&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  
  HB 2434 HB 1980 HB 1032 SB 6178 SB 6185 HB 1320 SB 5234 SB 5238 HB 2239 HB 2224 SB 6178 SB 6178 SB 6178 SB 5234 SB 5234 SB 5234 SB 5234 HB 1052 SB 5234 SB 5234 SB 5234
  </code></pre>
  
  <p>Replacement divs appear on top of the list of bills which is not what I want. I want each div to appear in place of the bill number it is replacing. Like this: </p>
  
  <pre><code>HB 2434 HB 1980 HB 1032 SB 6178 SB 6185 HB 1320 &lt;div&gt;&lt;h2&gt;SB 5234 Information&lt;h2&gt;&lt;p&gt;John Doe is the primary sponsor of this bill. The bill was introduced on January 1st, 2013 by Reps. Doe of Cooltown and Jane Smith of Anytown. It is scheduled for a hearing in the Committee that Doesn't Matter on March 18th, 2013. Comments regarding the bill may be directed to Rep. Doe.&lt;/p&gt; &lt;p&gt;Recent comments about the bill:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;First comment&lt;/li&gt;&lt;li&gt;Second comment&lt;/li&gt;&lt;li&gt;Third comment&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; SB 5238 HB 2239 HB 2224 SB 6178 SB 6178 SB 6178 SB 5234 SB 5234 SB 5234 SB 5234 HB 1052 SB 5234 SB 5234 SB 5234
  </code></pre>
  
  <p>Of course, the other bill numbers would also ultimately be replaced with divs. But replacing just one should result in the above.</p>