We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Show first few LI from UL html code?

Medium Priority
Last Modified: 2013-12-24
I am pulling html code from a database.
The code is inserted with a rich text editor on a text area,
and the client is formatting the text as a list of bullet points.

I want to show the first few <li> from the list,
and then insert a closing <ul>.

How can I grab just the beginning section from the example below,
cutting off just after the third closing </li>


<li>Three Bedroom Two Bath </li>  
<li>1772&nbsp; Square feet </li>  
<li>Two Car Garage </li>
<li>Covered Patio &nbsp; </li>
<li>Jetted Tube and Separate Shower in Master </li>
<li>Tile Surrounds in Bathroom </li>
<li>Tile Entry Way </li>
<li>10 feet Coffered Ceilings </li>
<li>Front and Rear Landscaped </li>

=============  I want to get/show this ============

<li>Three Bedroom Two Bath </li>  
<li>1772&nbsp; Square feet </li>  
<li>Two Car Garage </li>


Thanks in advance!

Watch Question

If this code is pulled from a database from without one field you can't use repeat regions that you can program to show 1 or 5 rows of <li></li> tags, so that's your problem right now i think ? ok:

1. you could change the method of storing these html code so that you insert one row: <li>Two Car Garage </li>  at a time so that you can use a repeat region that you will repeat between the <ul>  and </ul> tags, then you can program to show 1 or 5 of these rows

2. you could use something as #left(table.field,100)#, so that you just pull the first 100 characters, the problem then is that you have to be sure about the length of the <li></li> rules, otherwise maybe you will mis a closing </li> because that was on position 106 while your characters show up till 100...

I think 1 is the best solution...

Is this something you can work with? I can be more specific if you need?

Let me know!:)


Hi Marco.
It is all one field, with the code as shown above, and maybe even a few <p>whatever</p> in there too, depending how client uses the wysiwyg.

I dont want to use #left(whatever)# because the length is variable.

I need to use some type of code to find the third time the string </li> appears in the data, and drop everything after that.
Select the field from the database and have CF trim the variable:

<CFSET listitems = 3>
<CFSET s = 0>
<CFLOOP from="1" to="#listitems#" index="i">
      <CFSET s = refindnocase("</li>",mystring,s+1)>
<CFSET mystring = LEFT(mystring,s+4)>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Nice one:)

<cfquery name="test" datasource="test">
FROM db</cfquery>

<CFSET listitems = 3>
<CFSET s = 0>
<CFLOOP from="1" to="#listitems#" index="i">
     <CFSET s = refindnocase("</li>",#test.html#,s+1)>
<CFSET mystring = LEFT(#test.html#,s+4)>


This works great for me, all credits ofcourse to danrosenthal :)


Well done Dan - thanks so much!

Here is the page...

Here is my code...

( decided to go for 4 since you made it so easy!)

<CFSET listitems = 4>
<CFSET s = 0>
<CFLOOP from="1" to="#listitems#" index="i">
     <CFSET s = refindnocase("</li>",recordMainText,s+1)>
<CFSET recordTextSnip = LEFT(recordMainText,s+4)>


====== thanks again ======
No problem. Nice looking site!


Thanks Dan - the content is being entered through a combination of
my little CF CMS system (with a mySQL database) and straight-up html layout.
Your little bit of code was the icing on the cake for that 'listings' page, which is all being entered and maintained by the client.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.