List Info

Thread: how to transform a list form of hyphens to an html unordered list




how to transform a list form of hyphens to an html unordered list
user name
2006-02-09 14:55:41
Hi all,

I have a question which I have no ideal of the answer...I am
currently
working on a web application and at some time, I have a
string
representing a short text. This could be a simple example :

"This is my list :nr-list item 1nr-list item
2nr-list item
3nrnrThis was a great list."

Let's say this outputs like that :
-------------------------------------------
This is my list :
-list item 1
-list item 2
-list item 3

This was a great list.
-------------------------------------------

Would it be simple with regular expression to transform the
list formed
of hyphens("-") to an HTML unordered list? I mean
that the preceding
example would rather output as :
-------------------------------------------
This is my list :
<ul><li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li></ul>

This was a great list.
-------------------------------------------

I have some experience with regular expression but have not
a clue on
how to do this...I also see some problems as if the string
starts with
an hyphen, as it would also need to transform correctly,
event if it
doesn't have the /n/r before the hyphen("-").
e.g. "-list item 1/n/r- list item 2" should output
as :
-------------------------------------------
<ul><li>list item 1</li>
<li>list item 2</li><ul>
-------------------------------------------

And what about if there are two lists separated by some
carriage
returns?...As you can see, I really don't know where to
start with this
problem. :/

Thanks in advance,

ibiza

how to transform a list form of hyphens to an html unordered list
user name
2006-02-09 15:55:18
On 2/9/06, ibiza <lambertbgmail.com> wrote:
>
> -------------------------------------------
>
> Would it be simple with regular expression to transform
the list formed
> of hyphens("-") to an HTML unordered list?

What are you using to build the website?  Regex can do the
find/replace part, it's just a matter of knowing how to use
the regex
libraries in the scripting language you're using.  In php,
you'd want
to use the preg functions.

Really all you'd have to do is build a function that takes a
list in
hyphen format and returns it as html.  For example
function listMarkup($list) {
  $output = "<ul>n";
  $output .= preg_replace('#$-
(.*?)$#im','<li>$1</li>',$list);
  $list.="</ul>n";
  return $output;
}

Then all you have to do is run that function over each of
your lists,
which could be done by passing the entirety of your text
through like
this

$text = preg_replace('#(?:(^-
.*?)[\r\n]){2,}#ime','listMarkup($0)',$text);

Note the 'e' modifier in the regex, it's necessary to pass a
function
over the results directly as I did there.

I haven't actually tested this for accuracy, but it should
at least
give you an idea ;)

--
Remember, no matter where you go, there you are. -Buckaroo
Banzai
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )