Forum Moderators: coopster

Message Too Old, No Replies

need a little hellp with str_replace please

         

Smad

10:43 am on Jan 14, 2005 (gmt 0)

10+ Year Member



Hi all

I am trying to remove unwanted <li> tags from my html,
example.
<li>
<li>
<li>
<li>
<li>text
<li>text

i want just:

<li>text
<li>text

i have tried variants of this code, but i still cant get it to work, where am i going wrong?

$content = str_replace("<li>+","",$content);

TIA

coopster

11:54 am on Jan 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$content = str_replace("<li>\n",'',$content);
If you need to check for different forms of the newlines or multiples, you should probably use a regular expression and preg_replace() [php.net].

Smad

12:33 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



i think i have tried something like that. i will post my code as its still putting in those <li>'s

$content = $row['module_content'];
$content = htmlspecialchars($content);
$content = str_replace("\n","<li>",$content);
$content = str_replace("<li>\n",'',$content);

Thanks for help so far.

StupidScript

11:32 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're on a Windows system, you'll need to adjust coopster's suggestion:

Unix/Linux system:

$content = str_replace("<li>\n",'',$content);

Windows system:

$content = str_replace("<li>\r\n",'',$content);

Perhaps there are spacebars following the <li> and before the newline, i.e.

<li> \n
? If this is the case, you'll want to use
preg_replace()
instead (or you could go line by line and use
trim()
, but that's too tedious):

Unix/Linux:

$content = preg_replace("/<li>\s*\n/",'',$content);

Windows:

$content = preg_replace("/<li>\s*\r\n/",'',$content);

On Windows servers, a newline is a combination of a carriage return (

\r
) and a newline (
\n
).

I'm wondering what you are doing with this, too:

$content = str_replace("\n","<li>",$content);

It seems to be inserting an empty

<li>
everywhere there's a newline. Is that useful to you?

Smad

3:43 pm on Jan 15, 2005 (gmt 0)

10+ Year Member



all i want is the text entered into a text field on the form be displayed with some formatting, as what being entered is mostly one line key points i thought the best thing to do was make it a bullet list. but i wanted to cover the possibilty of enter being pressed a few times by mistake.

thanks for all the replies btw