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.

1

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

Comments

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>";

Comments

0

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

1 Comment

This doesn't smell like an answer; more like a misplaced comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.