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

需要使用PHP将文本字符串替换为一组外部文件的内容的帮助吗,php,curl,Php,Curl,我正在进行一个项目,以提高公众对立法机构内部工作的可访问性,我遇到了一个障碍。希望能得到一些帮助,因为我已经搜索了几个小时,却一无所获 基本上,我有一些数据要处理。源当前如下所示: <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

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

基本上,我有一些数据要处理。源当前如下所示:

<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>
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

另一天可能会是这样:

<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>
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>

HB 5324信息
约翰·多伊是这项法案的主要发起人。该法案由代表于2013年1月1日提出。Cooltown的Doe和Anytown的Jane Smith。计划于2013年3月18日在委员会举行一次无关紧要的听证会。有关该法案的评论可直接发送给能源部众议员。

关于该法案的最新评论:

  • 第一条评论
  • 第二条评论
  • 第三条评论
其他每一项法案都将被类似的法案所取代

顺序对于最终结果很重要,因为我希望显示在页面顶部的div与具有最近活动的票据相对应,而非活动票据div位于底部

使用PHP和cURL实现这一点的最佳方法是什么?我理解cURL的基本用法,但我希望包含源文件,然后依次替换每个账单号,用一个小文件替换,该文件只包含像上面那样包装在div中的内容。这些文件都存储在同一个位置,并且可以通过以下方式访问:

<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>

我一直试图用两张账单来解决这个问题,但我肯定我做错了:

<?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') ?>

像这样使用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");
$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 HB 1052 SB 5234 SB 5234 SB 5234';
$count=preg_match_all(“/([A-Z]+)([0-9]+)/”,$s,$matches);

对于($i=0;$i)确切的问题是什么?上面脚本的输出是什么?您的障碍在哪里?您可以使用
include('HB1980.html')吗
?障碍在于,账单需要按照其列出的顺序进行替换。并且顺序发生了变化。我现在所做的只是导致div出现在列表上方,而不是替换列表中的账单编号。因此,当“HB 1980”被替换时,替换div需要转到这里:……HB 2434[此处为替换分区]HB 1032 SB 6178 SB 6185 HB 1320 SB 5234 SB 5238…..HB 1980并不总是排名第二。有时它会更靠下,甚至可能排在榜首。这似乎是一种更明智的方式,但我需要在账单编号出现的准确位置用div替换账单编号。我们的想法是替换“HB 1980”和“HB 1032"依此类推。div必须以与源列表相同的顺序出现,源列表会频繁地重新生成。@Seascape PREG会按照它在字符串中找到字段的顺序返回字段,因此这会以正确的顺序返回字段,或者您的问题被误解。我的代码只打印e
include
s to。因此,您必须在某个地方有其他代码打印列表。CURL代码打印列表。但是如果我在下面包含您的代码,我会在账单列表上方得到div,div的顺序是根据它们在$s=''中的显示方式。我用附言更新了我的原始问题,以解释w我要买的帽子,所以你只想换一件?