Solved

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

Posted on 2013-11-26
2
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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