
December 22nd, 2011, 02:12 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
Hi Penzais,
As ManiacDan was saying, seeing sample data always helps write the expression.
In the meantime, if I understood your requirements, this code might work.
A. Replacing the first <li>
PHP Code:
$result = preg_replace('/(?is)<li>/', '<ol><li>', $string,1);
It's a simple preg_replace on a multiple-line string, case insensitive.
The key here is the last argument: the 1 says "make one replacement then stop".
B. Replacing the last </li>
PHP Code:
$result = preg_replace('%(?is).*</li>%', '</li></ol>', $string);
Here we don't need the "1". Because of the greedy star, the regex engine shoots all the way to the end of the string, then backtracks to match the very last </li>
The i and s modes are turned on inline but you could turn them on after the pattern string if you prefer.
I haven't tested those but am hopeful that I didn't introduce a bug of some kind. Give it a whirl and let me know how you go, happy to help you tweak it if it doesn't do the job.
|