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) は法案番号であり、法律を参照しています。順序は重要です。これらの請求書は、変更された日付順にリストされ、最後に変更された請求書が最初になります。ファイルが再生成されると、順序は定期的に変更されます。形式は変わりません。これは常に、空白で区切られた請求書のリストを含む単なるテキスト ファイルです。

上記の各請求書番号を外部ファイルに格納されているコンテンツに置き換え、そのコンテンツを Web ページに含めたいと考えています。各請求書番号に対応する説明情報を含む一連の外部ファイルがあります (つまり、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

私はこれを 2 つの請求書だけで機能させようとしてきましたが、これは間違っていると確信しています。

<?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 に置き換えられます。ただし、1つだけを置き換えると、上記の結果になります。

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 に答える