Search code examples
phpparsingstring-parsing

How to parse out punctuation and replace with html tags in PHP


I was wondering if there is a way to take a string like

$string = "Look at *me* because I am bold";

and replace the asterisks with tags so the string would be

echo $string; //"Look at <bold>me</bold> because I am bold"

Solution

  • Try $string = preg_replace('~\*(.*?)\*~','<bold>$1</bold>',$string);

    Edit: Appended the semicolon I forgot.