Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    125
    Thanks
    30
    Thanked 0 Times in 0 Posts

    Splitting a string into a bulleted list...

    Hello. I have a long string of text. Throughout the string, there is the string of characters "&&&&" (four ampersands). Whenever that "&&&&" shows up, I'd like to split the string, and put each split into a list...

    ...so, if the string were:

    Item 1&&&&Item 2&&&&Item 3

    Then I'd want to create this:

    Code:
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
    Can anyone help me with the code to do that??? Thanks!

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hold on. I wrote this in javascript, let me whip up a php version quickly.
    Here is the php version
    PHP Code:
    <?php
    function makeList($str)
    {
        
    $str split('&&&&',$str);
        
    $html  '<ul>';
        for(
    $i 0$i count($str); $i++)
        {
            
    $html .= '<li>'.$str[$i].'</li>';
        }
        
    $html .= '</ul>';
        return 
    $html;
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    echo makeList("Item 1&&&&Item 2&&&&Item 3");
    ?>
    </body>
    </html>
    Here is the JS version if you needed
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function makeList(str)
    {
    	str = str.split('&&&&');
    	createUL = document.createElement('ul');
    	for(var i = 0; i < str.length; i++)
    	{
    		var createLI = document.createElement('li');
    		var createText = document.createTextNode(str[i]);
    		createLI.appendChild(createText);
    		createUL.appendChild(createLI);
    	}
    	document.getElementById('listhold').appendChild(createUL);
    }
    window.onload = function()
    {
    	makeList('Item 1&&&&Item 2&&&&Item 3');
    }
    </script>
    </head>
    
    <body>
    <div id="listhold"></div>
    </body>
    </html>
    Last edited by _Aerospace_Eng_; 11-19-2007 at 09:42 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    ShootingBlanks (11-19-2007)

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    125
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Thanks!

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I think that's supposed to be in PHP.

    PHP Code:
    function bulletizeRaw($line) {
        
    $lines explode('&&&&'$line);
        
    $output '';
        if (
    sizeof($lines) > 0) {
            
    $output .= '<ul><li>' implode('</li><li>'$lines) . '</li></ul>';
        }
        return 
    $output;
    }

    $raw "Line 1&&&&Line 2&&&&Line 3";
    echo 
    bulletizeRaw($raw); 
    Beat you to it by 2 minutes

    Either way works I suppose.
    Last edited by aedrin; 11-19-2007 at 09:43 PM.

  • Users who have thanked aedrin for this post:

    ShootingBlanks (11-19-2007)

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    125
    Thanks
    30
    Thanked 0 Times in 0 Posts
    thanks, everyone!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •