Php 使用preg_replace修改SimplePie中项目的输出
我正在使用SimplePie来显示RSS提要中第一个项目的标题,当我被要求监控的立法机构中的法案发生问题时,RSS提要会进行更新。这个立法机构发布RSS提要,我可以用SimplePie获取并为需要这些信息的人显示。SimplePie代码完美地完成了它的工作 但是,我想在使用preg_replace对输出进行回显之前对其进行修改,以稍微清理一下 我最初使用的SimplePie代码如下:Php 使用preg_replace修改SimplePie中项目的输出,php,preg-replace,Php,Preg Replace,我正在使用SimplePie来显示RSS提要中第一个项目的标题,当我被要求监控的立法机构中的法案发生问题时,RSS提要会进行更新。这个立法机构发布RSS提要,我可以用SimplePie获取并为需要这些信息的人显示。SimplePie代码完美地完成了它的工作 但是,我想在使用preg_replace对输出进行回显之前对其进行修改,以稍微清理一下 我最初使用的SimplePie代码如下: <?php $max = $feed->get_item_quantity(1); f
<?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]+) –/';
$str = preg_replace('/([0-9]+) –/', '', $str);
?>
<?php echo $item->get_title(); ?>
<?php endfor; ?>
。。。但它并没有修改我的输出。它似乎什么也没做。我没有收到错误,但它不工作
实际输出(仅为项目标题)当前如下所示:
07–2013年3月1日–提交规则委员会二读
开头的两位数是无关信息。我想去掉它和它后面的连字符,所以标题应该是这样的:
2013年3月1日——提交规则委员会二读
虽然理想情况下,我认为应该是这样的:
(2013年3月1日)提交规则委员会二读
关于如何进行此操作的建议?请尝试以下内容:-
$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.
(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]+) –/'; <<======= your title string is regular expression here
// assign your title string here
$str = preg_replace('/([0-9]+) –/', '', $str);
?>
<?php echo $item->get_title(); ?>
<?php endfor; ?>
输出:-
March 1, 2013 – Passed to Rules Committee for second reading.
(March 1, 2013) – Passed to Rules Committee for second reading.
如果需要像这样修改代码,您使用的$str应该包含您希望修改的字符串
<?php $max = $feed->get_item_quantity(1);
for ($x = 0; $x < $max; $x++):
$item = $feed->get_item($x);
echo preg_replace('/([0-9]+) –/', '', $item->get_title()); ?>
<?php endfor; ?>
尝试过(必须将连字符设置为单连字符),效果良好!你建议用什么正则表达式在日期周围加上括号,使其看起来像这样?(2013年3月1日)提交规则委员会二读