2016-09-19 95 views
1

我有一個按日期排序的事件列表,每個事件都在<p>之內。有更多的200,它看起來像這樣(我已經設法清理一點點):顛倒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?

你們能幫我解決這個問題嗎?

謝謝!

+0

得到所有的段落,你需要到一個數組,然後運行磁盤陣列之前重新扭轉它們的功能輸出或向後走並穿過陣列輸出。使用這些信息,您可以將一些代碼放在一起,當出現問題時我們可以提供幫助。 – gabe3886

+0

你是如何繪製數據的?在該代碼中,您可以非常簡單地添加一個定義方法,但是您分享的代碼無法幫助您。我們在這裏失明。 –

+0

您的回答讓我想起了爆炸方法,並帶有preg_match_all函數。我會試一試。謝謝 ! – Cyc

回答

0

好吧,有時事情很簡單,但你需要向其他人解釋找到解決方案。所以它就是這樣。

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

太簡單了!

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

+0

程序化解決方案 –

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]));