所以现在我有了这个:<?php$separator1 = "\n";$separator2 = ":";$textarea = get_custom_field('my_custom_output');$array = explode($separator1,$textarea);$output = ''; // initialize the variableforeach ($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"的文本区域中使用以下内容:text1:text1-page-urltext2:new-text2-pagetext3:different-page-text3结果是text1text2text3已成功链接和设置样式. (我没有将它们链接,因为stackoverflow不允许我发布两个以上的链接,因为我只有3个代表).因此,我的下一个也是最后一个想要完成的任务是:text1description 1text2description 2text3description 3像以前一样链接text1等,但是不链接描述.因此,我将在stackoverflow中尽力尝试一下.但是,我希望我需要一些帮助.走吧:<?php$separator1 = "\n";$separator2 = ":";$separator3 = ";";$textarea = get_custom_field('my_custom_output');$array = explode($separator1,$textarea);$output = ''; // initialize the variableforeach ($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>,并在定义为"my_custom_output"的文本区域中使用以下内容:text1:text1-page-url;Text1 Descriptiontext2:new-text2-page;Description For Text2text3:different-page-text3;A Text3 Description我需要的输出是: text1 Text1说明 text2文字2的描述 .. etc我不知道分号是否可以工作,但是我不能使用空格(\ s),因为描述中有空格.我愿意接受建议. ================================================ =====我最新的尝试:<?php$separator1 = "\n";$separator2 = ":";$separator3 = ";";$textarea = get_custom_field('my_custom_output');$array = explode($separator1,$textarea);$output = ''; // initialize the variableforeach ($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>成功!!! = D 解决方案不确定我是否足够了解(我不确定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是每一行,就像这样:$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>';}This is tailing off my other question which was successfully answered: stackoverflow.com/questions/8597929/need-to-create-li-with-list-of-different-links-using-php-explode-methodso now I have this:<?php$separator1 = "\n";$separator2 = ":";$textarea = get_custom_field('my_custom_output');$array = explode($separator1,$textarea);$output = ''; // initialize the variableforeach ($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>and that, using the following in a textarea which is defined to "my_custom_output": text1:text1-page-urltext2:new-text2-pagetext3:different-page-text3and the result istext1text2text3which are successfully linked and styled. (i didnt make them links because stackoverflow doesn't let me post more than two links because i only have 3 rep).So my next and final desired task is to do this:text1description 1text2description 2text3description 3where the text1 etc are linked like before but the descriptions are not linked.So I will do my best, right here in stackoverflow, to try it. However, I expect I will need some help. Let's go:<?php$separator1 = "\n";$separator2 = ":";$separator3 = ";";$textarea = get_custom_field('my_custom_output');$array = explode($separator1,$textarea);$output = ''; // initialize the variableforeach ($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>and to use the following in the textarea which is is defined to "my_custom_output":text1:text1-page-url;Text1 Descriptiontext2:new-text2-page;Description For Text2text3:different-page-text3;A Text3 Descriptionand I need the output to be:text1Text1 Descriptiontext2Description For Text2..etcI don't know if semicolon will work, but I can't use a space (\s) because there are spaces in the description. I am open to suggestions.====================================================MY NEWEST TRY:<?php$separator1 = "\n";$separator2 = ":";$separator3 = ";";$textarea = get_custom_field('my_custom_output');$array = explode($separator1,$textarea);$output = ''; // initialize the variableforeach ($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 解决方案 Not sure if I understand this well enough (I'm not sure what get_custom_field() gets you - can't find that as a regular PHP function), but when you explode an item that has multiple instances of the delimiter, you'll get multiple arrays.So:$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.To work with your code..Assume each $item is each row at this point, like this:$item = "text1:text1-page-url;Text1 Description";Then this will do the trick: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>';} 这篇关于需要在php上添加第二行和第三个定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 22:12
查看更多