Need to add second line and third delimiter on php explode

别来无恙 提交于 2019-12-24 13:10:02

问题


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-method

so now I have this:

<?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>

and that, using the following in a textarea which is defined to "my_custom_output":

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

and the result is

text1
text2
text3

which 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:

text1
description 1
text2
description 2
text3
description 3

where 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 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>

and to use the following in the textarea which is is defined to "my_custom_output":

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

and I need the output to be:

  • text1

    Text1 Description

  • text2

    Description For Text2

  • ..etc

I 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 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


回答1:


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>';
}



回答2:


I'd slightly optimize the algorithm as there is no need for second explode(). Just separate substrings within one row with the same separators (and don't forget to escape that separator inside all your data):

<?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>

And here is the data format I suggest to use (separate all fields by :)

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


来源:https://stackoverflow.com/questions/8598377/need-to-add-second-line-and-third-delimiter-on-php-explode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!