Solved

Splitting a string and storing each individual word in <ul><li></li></ul>

Posted on 2013-11-26
2
270 Views
Last Modified: 2013-11-26
Hi

I have a requirement where i want to split a text into chunks and show them in <ul><li></li></ul>
e.g I have

<specs><![CDATA[
Capacity: 4.7GB Speed: 3x Pack Style: 5 Pack Cartridge T2 The Type 2 Cartridge is a removable case for DVD-RAM. This disc is preformated in UDF 2.0
]]>
</specs>

And I want to display it as

Capacity: 4.7GB
Speed: 3x
Pack Style: 5 Pack Cartridge T2
The Type 2 Cartridge is a removable case for DVD-RAM.This disc is preformated in UDF 2.0
0
Comment
Question by:srikanth_saladi
2 Comments
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 250 total points
ID: 39676909
Here you go: http://jsfiddle.net/WgjcK/

<specs><![CDATA[
Capacity: 4.7GB Speed: 3x Pack Style: 5 Pack Cartridge T2 The Type 2 Cartridge is a removable case for DVD-RAM. This disc is preformated in UDF 2.0
]]>
</specs>

<div id="result"></div>

Open in new window

function toList(value){
    var capacity = value.substring(value.indexOf('Capacity'), value.indexOf('Speed'));
    var speed = value.substring(value.indexOf('Speed'), value.indexOf('Pack Style'));
    var packStyle = value.substring(value.indexOf('Pack Style'), value.indexOf('The Type'));
    var final = value.substring(value.indexOf('The Type'), value.indexOf(']]'));
    
    var result = [];
    
    result.push('<ul>');
    result.push('<li>' + capacity + '</li>');
    result.push('<li>' + speed + '</li>');
    result.push('<li>' + packStyle + '</li>');
    result.push('<li>' + final + '</li>');
    result.push('</ul>');
    
    return result.join('');
}
    
var value = document.getElementsByTagName("specs")[0].innerHTML;

document.getElementById('result').innerHTML = toList(value);

Open in new window


Be aware that this is highly hardcoded...
To make this safer you had to at least have a proper property separator, without that it's quite impossible to create a generic regexp that parses that properly.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39677373
Completely agree with @AlexCode -- this would make a lot more sense if you have a delimiter to separate the parts that you want to put into the list items.  The comma or vertical pipe is a traditional delimiter.

Please see: http://www.laprbass.com/RAY_temp_sree.php

<?php // RAY_temp_sree.php
error_reporting(E_ALL);

// THE TEST DATA
$xml = <<<ENDXML
<?xml version="1.0" ?>
<specs><![CDATA[
Capacity: 4.7GB Speed: 3x Pack Style: 5 Pack Cartridge T2 The Type 2 Cartridge is a removable case for DVD-RAM. This disc is preformated in UDF 2.0
]]>
</specs>
ENDXML;

// THE SIGNAL STRINGS
$signals = array
( 'Capacity'
, 'Speed'
, 'Pack Style'
, 'The Type'
)
;
// MAKE AN OBJECT AND EXTRACT THE TEXT
$obj = SimpleXML_Load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$txt = (string)$obj[0];

// ITERATE OVER THE SIGNALS
$cnt = count($signals) - 1;
foreach ($signals as $key => $sig)
{
    // LOCATION OF THE BEGINNING SIGNAL
    $alpha = strpos($txt, $sig);

    // THE LAST SIGNAL
    if ($key == $cnt)
    {
        $substrs[] = substr($txt, $alpha);
    }
    // EVERYTHING BUT THE LAST SIGNAL
    else
    {
        $omega = strpos($txt, $signals[$key+1]);
        $substrs[] = substr($txt, $alpha, $omega-$alpha);
    }
}

// CREATE THE HTML DOCUMENT
$out = '<ul>' . PHP_EOL;
foreach ($substrs as $str)
{
    $out .= '<li>' . trim($str) . '</li>' . PHP_EOL;
}
$out .= '</ul>' . PHP_EOL;

// SHOW THE WORK PRODUCT
echo '<pre>';
echo htmlentities($out);

Open in new window

Best regards, ~Ray
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now