我正在开展一个项目,以改善公众对立法机构内部运作的可及性,我遇到了障碍。希望得到一些帮助,因为我已经搜索了几个小时而无处可去。
基本上,我有一些我想要操作的数据。源目前看起来像这样:
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.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
替换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替换。但是只更换一个就可以产生上述结果。
答案 0 :(得分: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");