I have a string that is formatted by symbols (symbols similar to those used to format questions on this site).
Rules:
**Hello** means bold = < b>Hello< /b>
*Hello\\ means bulleted list = < li>Hello< /li>
Hello\\ means line break = Hello< br>
I want to replace:
- Every first occurence of ** with < b> and every second ** with < /b>.
- The same for * with < li> and \\ with < /li>.
- All \\ that occur without a * somewhere in the string before, should be converted to < br>.
Example string:
$myString = 'Hello my **Friend**,\\here is the stuff you need to buy for me:*knife\\*water bottle\\***fake ID**\\\\\\Thank you in advance and don not forget the **fake ID**!\\Sincerely yours\\Eddy'
Note: This style is not my invention. It is in use and I have to convert it.
I preg_match()-ed parts of it to get the stuff between the tags.
$myString = 'Hello my **Friend**,\\here is the stuff you need to buy for me:*knife\\*water bottle\\***fake ID**\\\\\\Thank you in advance and don not forget the **fake ID**!\\Sincerely yours\\Eddy';
$result = array();
$firstBold = '<b>'. preg_match('~\*\*(.*?)\*\*~', $myString, $firstBold) . </b>;
$result += $firstBold
// and so on...
(Ignore mistakes in this, its written from memory)
I didn't consider the words before the first bold, but it's basically the same. This will get the job done at the end but it seems cumbersome to me. I am in search for a more elegant way to do this.
What is the best way to solve this in PHP?