...

View Full Version : Splitting a string into a bulleted list...



ShootingBlanks
11-19-2007, 09:13 PM
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:


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

_Aerospace_Eng_
11-19-2007, 09:35 PM
Hold on. I wrote this in javascript, let me whip up a php version quickly.
Here is the php version

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

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

ShootingBlanks
11-19-2007, 09:40 PM
Thanks!

aedrin
11-19-2007, 09:40 PM
I think that's supposed to be in PHP.



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.

ShootingBlanks
11-19-2007, 09:48 PM
thanks, everyone!:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum