反转200个段落的顺序

时间:2016-09-19 13:40:14

标签: javascript php sorting

我有按日期排序的事件列表,每个事件都在<p>之内。有200多个它看起来像这样(我已经设法清理了一点):

<p><strong>1 july 2011</strong> event 1<br>
Place where the event 1 takes place<br>
more infos about event 1<br>
event more infos about event 1</p>
<p><strong>2 september 2011</strong> event 2<br>
Place where the event 2 takes place<br>
more infos about event 2<br>
event more infos about event 2</p>
<p><strong>3 september 2011</strong> event 3<br>
Place where the event 3 takes place<br>
more infos about event 3<br>
event more infos about event 3</p>

它们目前按日期排序(最早的事件排在第一位),我想要做的是颠倒顺序以便先获得最新的事件。

我唯一的标记是<p>,其中每个事件都被封装。日期并不总是相同的格式。

我用excel,notepad ++和他的正则表达式尝试了一些东西,但是有可能与PHP和/或JS有关?

你们可以帮助我吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

在javascript中你可以在每个段落中读取,然后以相反的顺序迭代它们。这是你追求的那种。

  $(function() {

  var paras = $('#container p');
  var newhtml = "";
  for(var i=paras.length-1;i>=0;i--)
  {
      newhtml += $(paras[i]).html() + "</br>";
  }
  $('#container').html(newhtml);
});

Example Fiddle

答案 1 :(得分:0)

好的,有时事情很简单但你需要向别人解释才能找到解决方案。就是这样。

$string = "<p>blablabla</p><p>blablabla 2</p>";
$text = str_replace('</p>', '', $string);
$array = explode('<p>', $text);
$array = array_reverse($array);

这么简单!

答案 2 :(得分:0)

如果您想使用reg-exp:

$a = '<p><strong>1 july 2011</strong> event 1<br>
Place where the event 1 takes place<br>
more infos about event 1<br>
event more infos about event 1</p>
<p><strong>2 september 2011</strong> event 2<br>
Place where the event 2 takes place<br>
more infos about event 2<br>
event more infos about event 2</p>
<p><strong>3 september 2011</strong> event 3<br>
Place where the event 3 takes place<br>
more infos about event 3<br>
event more infos about event 3</p>';

$a = str_replace("\n","", $a); // maybe you dont need this line it happened to me couse of copy-paste from your question text
preg_match_all("/\<p\>(.*)\<\/p\>/U", $a, $m);

var_dump(array_reverse($m[1]));