需要帮助使用PHP将文本字符串替换为一组外部文件的内容


Need help using PHP to replace text strings with the contents of a set of external files

我正在进行一个项目,以提高公众对立法机构内部工作的可访问性,但我遇到了障碍。希望能得到一些帮助,因为我已经找了几个小时,却一无所获。

基本上,我有一些数据需要处理。目前的来源如下:

HB 2434 HB 1980 sb5234 sb6185 HB 1320 sb5238 HB 2239 HB 2224 HB 1052 HB 1032 sb6178 sb6185 sb1320

改天可能会是这样的:

sb5234 sb6185 hb1320 sb6178 sb6185 sb5238 hb2239 hb2224 hb1980 hb1032 hb1320 hb1052 hb2434

每一个(即HB 2434或SB 5324)都是一个法案编号,指的是一项立法。顺序很重要——这些账单按修改日期列出,最新修改的账单排在第一位。在重新生成文件时,顺序会定期更改。格式不变;它总是只是一个文本文件,其中有一个用空格分隔的账单列表。

我想用外部文件中的内容替换上面列出的每个账单编号,然后将该内容包含在网页上。我有一组外部文件,其中包含与每个票据编号相对应的解释信息(即SB5324.html)

因此,"SB5324"将被SB5324.html的内容所取代,类似于以下内容:

<div>
  <h2>HB 5324 Information<h2>
  <p>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.</p> <p>Recent comments about the bill:</p>
  <ul>
    <li>First comment</li><li>Second comment</li><li>Third comment</li>
  </ul>
</div>

其他每一项法案都将被类似的法案所取代。

顺序对最终结果很重要,因为我希望出现在页面顶部的div与最近活动的账单相对应,而不活动的账单div位于底部。

使用PHP和cURL最好的方法是什么?我理解cURL的基本用法,但我想包括源文件,然后用一个小文件按顺序替换每个账单编号,这个小文件只包含像上面一样包装在div中的内容。文件都存储在同一个地方,并且可以像这样访问:

http://website.tld/bills/divs/SB5324.html和http://website.tld/bills/divs/HB1980.html

我一直试图用其中两张账单来实现这一点,但我确信我错了:

<?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 "<p>Sorry, can't show the bills! Try refreshing the page.</p>"; } 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; }}?>
<?php getBills('MyBillList.txt') ?>

我是PHP的新手,所以希望能给我一些建议。谢谢

POSTSCRIPT:我现在的PHP代码正在生成以下内容:

<div>
  <h2>SB 5234 Information<h2>
  <p>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.</p> <p>Recent comments about the bill:</p>
  <ul>
    <li>First comment</li><li>Second comment</li><li>Third comment</li>
  </ul>
</div>
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

替换divs出现在账单列表的顶部,这不是我想要的。我希望每个div都出现在它要替换的票据编号的位置。像这样:

HB 2434 HB 1980 HB 1032 SB 6178 SB 6185 HB 1320 <div><h2>SB 5234 Information<h2><p>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.</p> <p>Recent comments about the bill:</p><ul><li>First comment</li><li>Second comment</li><li>Third comment</li></ul></div> 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

当然,其他票据编号最终也将被divs取代。但只替换一个应该会产生上述结果。

像这样使用regexp怎么样?

$s='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';
$count=preg_match_all('/([A-Z]+) ([0-9]+)/', $s, $matches);
for($i=0; $i<$count; $i++)
  include("{$matches[1][$i]}{$matches[2][$i]}.html");