1

我正在开展一个项目,以提高公众对立法机构内部运作的可及性,但我遇到了障碍。希望能得到一些帮助,因为我一直在寻找几个小时却一无所获。

基本上,我有一些我想要操作的数据。源目前看起来像这样:

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

在另一天,它可能看起来像这样:

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

其中每一个(即 HB 2434 或 SB 5324)都是一个法案编号,指的是一项立法。顺序很重要 - 这些帐单按修改日期列出,最近修改的帐单在前。随着文件的重新生成,顺序会定期更改。格式不变;它始终只是一个文本文件,其中包含由空格分隔的账单列表。

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

因此“SB 5324”将被替换为 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.htmlhttp://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

替换 div 出现在账单列表的顶部,这不是我想要的。我希望每个 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

当然,其他的账单号码最终也会被 div 取代。但是只替换一个应该会导致上述结果。

4

1 回答 1

0

像这样使用正则表达式怎么样?

$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");
于 2013-02-23T10:08:33.663 回答