Google Answers Logo
View Question
 
Q: PHP Regular Expression ( No Answer,   2 Comments )
Question  
Subject: PHP Regular Expression
Category: Computers > Programming
Asked by: larking-ga
List Price: $25.00
Posted: 10 Dec 2005 01:24 PST
Expires: 17 Dec 2005 03:56 PST
Question ID: 603993
I need a regex to solve the following situation.
A satisfactory answer will be PHP code that takes a string as an input
and returns the same string with problem resolved.

I have a block of html that has a group of list elements (<li>Bullet
Text</li>) which are added using an html text editor.  The text editor
is not adding the required surrounded <ul> and </ul>.  The regex
should therefore search a string for sets of list elements that do not
have surrounding <ul> tags and add them.
NB: 
-if there are <ul> tags then no more should be added.
-there may be more than one set of lists
-sets of <ol> tags should be ignored
-<li> tags may or may not be on seperate lines 
-the code will be tested using the following string: note the line line breaks


<P ALIGN="LEFT">this is some text<br/><br/>
<h2>Stay Safe</h2><br/>
<B>Residential care</B><br/><br/>this is some text<br/></P>
<LI>list item 1</LI><LI>list item 2</LI><LI>list item
3</LI><LI></LI><P ALIGN="LEFT">Our staff are blah blah</P><LI>list
item 1</LI>

<LI>list item 2</LI><LI>list item 3</LI>some more text might start straight away

- the code should return the following string

<P ALIGN="LEFT">this is some text<br/><br/>
<h2>Stay Safe</h2><br/>
<B>Residential care</B><br/><br/>this is some text<br/></P>
<UL><LI>list item 1</LI><LI>list item 2</LI><LI>list item
3</LI><LI></LI></UL><P ALIGN="LEFT">Our staff are blah
blah</P><UL><LI>list item 1</LI>

<LI>list item 2</LI><LI>list item 3</LI></UL>some more text might
start straight away

Clarification of Question by larking-ga on 10 Dec 2005 01:27 PST
when i say <ol> lists should be ignored i mean to say they should be
left intact and nothing should be changed about them
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP Regular Expression
From: iolog-ga on 10 Dec 2005 17:38 PST
 
The following solution works if you can live with whitespace being
altered between <LI> tags:

$input = preg_replace("/(<\/li>)\s*(<li>)/i","$1$2",$input);
$input = preg_replace("/(?<!li>)<li>/i","<UL><LI>",$input);
$input = preg_replace("/<\/li>(?!<li>)/i","</LI></UL>",$input);

Step 1 closes the whitespace between <LI> tags
Step 2 matches any <LI> tag which is not preceeded by a closing </LI>
tag and inserts a start <UL> through replacement.
Step 3 does the reverse, adding closing <UL> tags.

Running the above creates the string:

<P ALIGN="LEFT">this is some text<br/><br/>
<h2>Stay Safe</h2><br/>
<B>Residential care</B><br/><br/>this is some text<br/></P>
<UL><LI>list item 1</LI><LI>list item 2</LI><LI>list item
3</LI><LI></LI></UL><P ALIGN="LEFT">Our staff are blah blah</P><UL><LI>list
item 1</LI><LI>list item 2</LI><LI>list item 3</LI></UL>some more text
might start straight away

Which matches your test case (but removes whitespace between LI).  Let
me know if I can improve this solution for you.
Subject: Re: PHP Regular Expression
From: larking-ga on 17 Dec 2005 03:54 PST
 
Thanks, that is perfect. Works like a charm :D

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy