$re = '/[^[:graph:]*](?#Do not match on any visible character)
\K\*(?#Reset starting point and then match on a *)
([[:alpha:].,\ ]+(?:\*{2}[[:alpha:].,\ ]+\*{2}?\ ?)?)(*COMMIT)
\*\ ?(?#Match on ending * then stop matching with COMMIT)/xm';
$str = 'This text is not italic.
*This text is italic.*
This text is *partially* italic
This text has *two* *italic* bits
**bold text (not italic)**
**bold text with *italic* **
**part bold,** *part italic*
*italic text **with bold** *
*italic* **bold** *italic* **bold**
*invalid markdown (do not parse)**
random * asterisk';
$subst = "<i>$1</i> ";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php