Jump to content

Removing text between <LI> tags


jwhite68

Recommended Posts

Can anyone recommend how I can remove any text that lies between the <LI> tags. eg if its :

 

<LI class=MsoNormal style="BACKGROUND: white; MARGIN: 0cm 0cm 0pt; COLOR: black; tab-stops: list 36.0pt; mso-list: l0 level1 lfo1; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto">

 

I want to strip out everything to leave only <LI>.  I assume something like preg_replace, but can anyone advise how?

Link to comment
https://forums.phpfreaks.com/topic/71056-removing-text-between-tags/
Share on other sites

<pre>
<?php
$str = '<UL><LI class=MsoNormal style="BACKGROUND: white; MARGIN: 0cm 0cm 0pt; COLOR: black; tab-stops: list 36.0pt; mso-list: l0 level1 lfo1; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto">Text</LI></UL>';
echo preg_replace('/<(li)[^>]*>/i', '<\1>', $str);
?>
</pre>

The source text I have (from a client) seems to miss the end </LI> tags. Heres part of it.  I tried your code, but it doesnt strip the other tags within the LI tag:

 

<UL type=disc>
<LI class=MsoNormal style="BACKGROUND: white; MARGIN: 0cm 0cm 0pt; COLOR: black; tab-stops: list 36.0pt; mso-list: l0 level1 lfo1; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-ansi-language: EN-US; mso-bidi-font-size: 8.5pt">1x 1 Sudio apartment</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 8.5pt"><o:p></o:p></SPAN> 
<LI class=MsoNormal style="BACKGROUND: white; MARGIN: 0cm 0cm 0pt; COLOR: black; tab-stops: list 36.0pt; mso-list: l0 level1 lfo1; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 8.5pt">1</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-ansi-language: EN-US; mso-bidi-font-size: 8.5pt">2</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 8.5pt"> x 2 Double bedroom apartments<o:p></o:p></SPAN></LI></UL> 

 

All I want to keep is the text content within LI tags.  not any of the class, style etc attributes.

Can you explain to me what the '<\1>' does in one of the arguments. I dont understand the presence of '1'.

 

That is a backreference.

 

I tried your code, but it doesnt strip the other tags within the LI tag:

 

You didn't specify this. To better approach this, are there any attributes that you want to allow and if so, in which tags?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.