2011-12-22 86 views
1

這是拖尾這是成功回答了我的其他問題:stackoverflow.com/questions/8597929/need-to-create-li-with-list-of-different-鏈接-使用-PHP-爆炸法需要補充的第二行和第三分隔符在PHP爆炸

所以我現在這樣的:

text1:text1-page-url 
text2:new-text2-page 
text3:different-page-text3 

<?php 
$separator1 = "\n"; 
$separator2 = ":"; 
$textarea = get_custom_field('my_custom_output'); 
$array = explode($separator1,$textarea); 
$output = ''; // initialize the variable 
foreach ($array as $item) { 
    list($item_text, $item_links) = explode($separator2, trim($item)); 
    $output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>'; 
} 
?> 

<ul> 
<?php print $output; ?> 
</ul> 

並且,使用在其被定義爲 「my_custom_output」 一個textarea以下和結果我s

text1 
text2 
text3 

它們被成功鏈接和設計。 (我沒有讓他們的鏈接,因爲stackoverflow不讓我發佈超過兩個鏈接,因爲我只有3代表)。

所以我的未來和希望的最終任務是要做到這一點:

text1 
description 1 
text2 
description 2 
text3 
description 3 

其中的text1等鏈接像以前一樣,但這些描述沒有聯繫。

所以我會盡我所能,就在這裏的計算器,來試試吧。不過,我希望我會需要一些幫助。讓我們去:

<?php 
$separator1 = "\n"; 
$separator2 = ":"; 
$separator3 = ";"; 
$textarea = get_custom_field('my_custom_output'); 
$array = explode($separator1,$textarea); 
$output = ''; // initialize the variable 
foreach ($array as $item) { 
    list($item_text, $item_links) = explode($separator2, trim($item)); 
    $output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>'; 
} 
?> 

<ul> 
<?php print $output; ?> 
</ul> 

,並使用下列其中是定義textarea的以 「my_custom_output」:

text1:text1-page-url;Text1 Description 
text2:new-text2-page;Description For Text2 
text3:different-page-text3;A Text3 Description 

,我需要的輸出是:

  • text1

    文本1說明

  • text2

    說明對於文本2

  • ...等

我不知道是否會分號的工作,但我不能使用空格(\ S),因爲有空間在描述中。我樂於接受建議。

============================================== ======

我的最新嘗試:

<?php 
$separator1 = "\n"; 
$separator2 = ":"; 
$separator3 = ";"; 
$textarea = get_custom_field('my_custom_output'); 
$array = explode($separator1,$textarea); 
$output = ''; // initialize the variable 
foreach ($array as $item) { 
    $itemarray = explode($separator2, trim($item)); 
    $item_text = $itemarray[0]; 
    list($item_links, $item_desc) = explode($separator3,$itemarray[1]); 

    $output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>'; 
} 
?> 

<ul> 
<?php print $output; ?> 
</ul> 

IT WORKS! = d

+0

會使用逗號工作(遵循典型的CSV)? – mc10 2011-12-22 01:17:38

+0

我這麼認爲。感謝您的建議!分號看起來像它也可以工作。 =)可能很好,如果我想添加第四個分隔符8) – 2011-12-22 01:34:51

回答

0

不知道如果我理解這一點不夠好(我不知道什麼get_custom_field()讓你 - 無法找到作爲常規PHP函數),但是當你爆炸擁有的多個實例的項目分隔符,你會得到多個數組。

所以:

$textarea = "text1:text1-page-url;Text1 Description"; 
$data = explode(':',$textarea); 
// at this point $data[0] will contain "text1", while $data[1] contains text1-page-url;Text1 Description" 
$descarray = explode(';',$data[1]); 
// then $descarray[0] contains "text1-page-url" and $descarray[1] contains "Text1 Description" so you can echo this out however you like. 

要與您的代碼工作.. 假設每個$產品每排在這一點上,像這樣:

$item = "text1:text1-page-url;Text1 Description"; 

,那麼這將這樣的伎倆:

foreach ($array as $item) { 
    $itemarray = explode($separator2, trim($item)); 
    $item_text = $itemarray[0]; 
    list($item_links, $item_desc) = explode(';',$itemarray[1]); 

    $output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>'; 
} 
+0

get_custom_field()只是調用Wordpress的自定義字段,它允許我爲my_custom_output提取數據(「my_custom_output」僅僅是我爲彌補我的自定義代碼是用來輸入數據的,你可以將它命名爲任何東西,比如mr_smith_0001,它是一個變量,另一方面,get_custom_field()是一個常量) – 2011-12-22 01:26:15

+0

我們可以堅持我當前的方法,所以我可以更好地學習嗎?謝謝。我真的不明白你的答案。 – 2011-12-22 01:30:34

+0

或者如果這可行,請告訴我如何使用我已有的。謝謝。 – 2011-12-22 01:33:16

0

我稍微優化了算法,因爲不需要秒explode()。 (不要忘記逃避所有數據內部的分離器)一行在短短獨立子具有相同分隔:

<?php 
$row_separator = "\n"; 
$line_separator = ":"; 
$textarea = get_custom_field('my_custom_output'); 
$array = explode($row_separator, $textarea); 
$output = ''; // initialize the variable 
foreach ($array as $item) { 
    list($item_text, $item_links, $item_desc) = explode($line_separator, trim($item)); 
    $output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>'; 
} 
?> 

<ul> 
<?php print $output; ?> 
</ul> 

這裏是數據格式,我建議使用(單獨由:所有字段)

text1:text1-page-url:Text1 Description 
text2:new-text2-page:Description For Text2 
text3:different-page-text3:A Text3 Description