简体   繁体   中英

Regex how to find start of the line and end of line

What would be the regex to find the start and end of texts and write a php function.

Example texts:

Juice Extractor
1L Juice Jug
Easy Operating
2 Speed Settings
Stainless Steel Blade
Safety Lock
Detachable Parts
Dishwasher Safe

I would like to find start and end to add <li></li> to each line using php function/regex.

So return result should look like:

<li>Juice Extractor</li>
<li>1L Juice Jug</li>
<li>Easy Operating</li>
<li>2 Speed Settings</li>
<li>Stainless Steel Blade</li>
<li>Safety Lock</li>
<li>Detachable Parts</li>
<li>Dishwasher Safe</li>

Thanks

Try something like this:

$text = "Juice Extractor
1L Juice Jug
Easy Operating
2 Speed Settings
Stainless Steel Blade
Safety Lock
Detachable Parts
Dishwasher Safe";

$rows = explode("\n", $text);
$html = implode("</li>\n<li>", $rows);
$html = "<li>" . $html . "</li>";

echo $html;

PS: If you want to use browser new lines, you should replace \n with <br> in implode . If the input text is with <br> instead of \n , you should replace that too in explode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM