i have 2 text string in php and i want to change them to html tags
first :
*.some text
* some text
and second :
#.sometext
# sometext
i want to change the first string that start with '*.' or '* ' to <li> and end with '\r' or '\n' to </li>
and i want to change the second string that start with '#.' or '# ' to <ol> and end with '\r' or '\n' to </ol>
I wrote this regex code but not work:
$regex1 = "#([*.])(.*)([\n])#e";
$text = preg_replace($regex1,"('<li>$2</li>')",$text);
$regex2 = "#([#.])(.*)([\n])#e";
$text = preg_replace($regex2,"('<ol>$2</ol>')",$text);
what should i do?
thanks a lot
I recommend quite a few changes to your code, which I’ll explain below.
First the code:
and the output
Changes:
emodifier in the regex*.and*, just specify*(escaped to\*, and an OR condition for.(escaped to\.) and..*, i.e..*?so you only eat the least possible text.PS: This probably isn’t what you wanted (ol has this structure:
<ol><li>...</li><li>...</li></ol>, but I wrote the code to your question’s spec. You should be able to modify this.