How to parse bold text to html in PHP programming language

  bold, markdown, parsing, php, regex

I’m trying to solve a question that asks me to translate a single line of markdown parser function to HTML and that supports bold text. I have written code to support headers (seen in my code below) and now need it to support bold text. I attached an image of the error I’m receiving for more clarity but essentially, my code is outputting asterisks (**) rather than actual bold text, or at least that’s what I gathered from looking at the error.

function markdown_parser($markdown)
{
  $markup = preg_replace_callback('/^(#+)(.+?)n/',
   function ($matches) use ($markdown) {
    if ($matches[1] == "") {
     return trim($markdown);
      }
    if (!preg_match('/s/', $matches[2])) {
    return "#" . $matches[2];
        }
  $hash_count = strlen($matches[1]);
   if (! in_array($hash_count, range(1, 6), true)) {
    return trim($markdown);
        }
    return html_entity_decode("<h$hash_count>" . trim($matches[2]) . "</h$hash_count>");
    },
    ltrim($markdown) . "n"
);
    return trim(preg_replace('~[rn]+~', ' ', $markup));
}

Code Error Image

Source: Ask PHP

LEAVE A COMMENT