2011-12-22 89 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