0

So google has failed me again (a rare thing). What I am trying to do is this:

I have a form that an admin will fill out where they fill out a text area, they separate bulleted text with a " ; " so they would say blah blah; blah blah blah; blah which would make an unordered list with 3 list elements. That's the logic anyways. The issue is trying to output this list... this is what I have so far:

the html (don't worry ill make an external style sheet if i can get this to work):

    <ul style='list-style:none;'>
       <?php echo $resp3; ?>
    </ul>

the php:

    $resp1 = "<li> $resp </li>";
        echo "1 = " . $resp1 . "";
    $resp2 = str_replace('$resp1', ';', ' </li><li> ');
        echo "2 =  " . $resp2 . "";
    $resp3 = substr('$resp2', 0, -4);
        echo "3 =  " . $resp3 . "";

I'm echoing to test where the failure is, $resp1 works apparently, $resp2 is outputting one bullet, nothing after it and $resp3 is blank. I've tried all sorts of things, it appears $resp3 may not work with a dynamic variable, but str_replace in $resp2 should work from what I've read.

4

3 回答 3

1

Why not just use explode?

$pieces = explode(';',$resp);
foreach($pieces as $piece) {
    $resp3 .= "<li>$piece</li>";
}

Or just use str_replace once:

$resp3 = str_replace(';','</li><li>',$resp);
$resp3 = "<li>$resp3</li>";
于 2013-06-20T15:27:10.270 回答
1

Here's a code snippet that'll work for you, just by using explode() to retrieve each string separated by ; and trim() to make sure the extra blanks before each string aren't echoed:

<?php
$textarea = "blah blah; blah blah blah; blah";
$items    = explode(';', $textarea);

echo "<ul>\n";
foreach ($items as $item) {
    echo "\t<li>", trim($item), "</li>\n";
}
echo "</ul>\n";

Output:

<ul>
    <li>blah blah</li>
    <li>blah blah blah</li>
    <li>blah</li>
</ul>
于 2013-06-20T15:29:20.200 回答
0

do not use variables in quotes! EDIT: and check the usage of str_replace

于 2013-06-20T15:24:47.743 回答