需要在php上添加第二行和第三个定界符 [英] Need to add second line and third delimiter on php explode

查看:56
本文介绍了需要在php上添加第二行和第三个定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这结束了我的另一个已成功回答的问题:stackoverflow.com/questions/8597929/need-to-create-li-with-list-different-links-using-php-explode-method

所以现在我有了这个:

<?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"的文本区域中使用以下内容:

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

结果是

text1
text2
text3

已成功链接和设置样式. (我没有将它们链接,因为stackoverflow不允许我发布两个以上的链接,因为我只有3个代表).

因此,我的下一个也是最后一个想要完成的任务是:

text1
description 1
text2
description 2
text3
description 3

像以前一样链接text1等,但是不链接描述.

因此,我将在stackoverflow中尽力尝试一下.但是,我希望我需要一些帮助.走吧:

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

,并在定义为"my_custom_output"的文本区域中使用以下内容:

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

我需要的输出是:

我不知道分号是否可以工作,但是我不能使用空格(\ 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>

成功!!! = 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-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

解决方案

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上添加第二行和第三个定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆