1

SimplePieを使用して、監視を求められた立法機関の法案に何かが起こったときに更新されるRSSフィードの最初のアイテムのタイトルを表示しています。この立法機関は、SimplePieで取得して情報を必要とする人々に表示できるRSSフィードを公開しています。SimplePieコードはその仕事を完璧に行っています。

ただし、出力をpreg_replaceを使用してエコーする前に変更して、少しクリーンアップしたいと思います。

動作する元のSimplePieコードは次のとおりです。

<?php $max = $feed->get_item_quantity(1); 
      for ($x = 0; $x < $max; $x++): 
          $item = $feed->get_item($x); 
?>
<?php echo $item->get_title(); ?>
<?php endfor; ?>

私はこれを使ってみました:

<?php $max = $feed->get_item_quantity(1); 
      for ($x = 0; $x < $max; $x++): 
          $item = $feed->get_item($x); ?>
<?php $str = '/([0-9]+) &#8211;/'; 
      $str = preg_replace('/([0-9]+) &#8211;/', '', $str); 
?>
<?php echo $item->get_title(); ?>
<?php endfor; ?>

...しかし、それは私の出力を変更していません。何もしていないようです。エラーは発生していませんが、機能していません。

実際の出力(単なるアイテムタイトル)は現在、次のようになっています。

07 – 2013年3月1日–2回目の読み取りのためにルール委員会に渡されました。

最初の2桁の数字は無関係な情報です。それとそれに続くハイフンを削除したいので、タイトルは次のようになります。

2013年3月1日–2回目の閲覧のためにルール委員会に渡されました。

ただし、理想的には、次のようになります。

(2013年3月1日)2回目の読書のために規則委員会に渡されました。

これを機能させる方法についての提案はありますか?

4

2 に答える 2

0

これを試してください:-

$str = '07 – March 1, 2013 – Passed to Rules Committee for second reading.'; 
$str = preg_replace('/(^[0-9]+) –/', '', $str);
echo $str;

出力:-

 March 1, 2013 – Passed to Rules Committee for second reading.

あなたのコード:-

    <?php $max = $feed->get_item_quantity(1); 
          for ($x = 0; $x < $max; $x++): 
              $item = $feed->get_item($x); ?>
    <?php $str = '/([0-9]+) &#8211;/';  <<======= your title string is regular expression here
          // assign your title string here  
          $str = preg_replace('/([0-9]+) &#8211;/', '', $str); 
    ?>
    <?php echo $item->get_title(); ?>
    <?php endfor; ?>  

更新されたコード:-

    <?php 
                $max = $feed->get_item_quantity(1); 
                for ($x = 0; $x < $max; $x++): 
                        $item = $feed->get_item($x); 
                        $title_str = $item->get_title();
                        $title = preg_replace('/(^[0-9]+) –/', '', $title_str); 

                        $pattern = '/(\w+) (\d+), (\d+)/i';
                        $replacement = '(${1} 1, $3)';
                        $title = preg_replace($pattern, $replacement, $title);  
                        echo $title;  
                endfor; 
     ?>

希望する出力の場合:-

$string = 'March 1, 2013 - Passed to Rules Committee for second reading.';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '(${1} 1, $3)';
echo preg_replace($pattern, $replacement, $string);

出力:-

  (March 1, 2013) – Passed to Rules Committee for second reading.
于 2013-03-05T04:32:31.783 に答える
0

このようにコードを変更する必要があります。使用した$strには、変更したい文字列が含まれている必要があります。

<?php $max = $feed->get_item_quantity(1); 
      for ($x = 0; $x < $max; $x++): 
          $item = $feed->get_item($x); 
 echo preg_replace('/([0-9]+) &#8211;/', '', $item->get_title()); ?>
<?php endfor; ?>
于 2013-03-05T04:43:29.353 に答える