Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace with lists

Status
Not open for further replies.

alphacooler

Programmer
Aug 29, 2005
73
US
I would like to wrap <li></li> tags around each newline.

How would I go about this?
 
Try this
taking the following text as an example:

This is some text
With Some line brakes
and some other lines
and another one.


You could do something like:

Code:
$textvariable="This is some text..."; [green]\\variable that contains the string to convert.[/green]


$output=str_replace("[red]\n\r[/red]","</li><li>",$texvariable);
[green]\\replace the carriage returns or end of line with the <li></li>  html for the lists."\n\r" are the characters for carriage return or new line [/green]


$output="<ul><li>" . $output ."</li></ul>";
[green]Finally add the beggining and ending <li> and if neccessary the <ul> and </ul> tags for the proper output of the list.[/green]
and here is the PHP online manual entry for the [blue] str_replace()[/blue] function.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
or with preg_replace()
Code:
<?php
$a='this is some text
this is text2
this is line 3
this is part4
this is the last line';

        $search[0]='/^/';
        $search[1]='/\n/';
        $search[2]='/$/';
        $repl[0]='<li>';
        $repl[1]='</li><li>';
        $repl[2]='</li>';
        $b=preg_replace($search,$repl,$a);
        echo "$b";
?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top