使用preg_replace的正则表达式
问题描述:
Im trying to create regular expression for preg_replace in php with
Input: this us * action (if) * fsfsffs
Output: <b id="action"> * action (if) * </b>
So far I'm here,
$text = " this us * action (if) * fsfsffs"
preg_replace( '#^(\s*)(\*)([^<>
]+)(\*)(\s*)$#m', '$1<b id="$3">$2 $3 $4</b>$5', $text );
Output for same i get is,
<b id=" action (if) ">* action (if) *</b>
答
Something like this should work:
preg_replace('#\*\s+([a-z]+)\s+\(([a-z]+)\)\s+\*#', '<b id="$1">* $1 ($2) *</b>', $input);