سؤال

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*)(\*)([^<>\n]+)(\*)(\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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top