NaiTreNo
NaiTreNo

Reputation: 49

Use Preg_match for just for string inside two certain symbols

I'm trying to use preg_match just for string inside two certain symbols for example

My Name is %^i%Ibrahem ^i.

I want to use preg_match just for ^i which is inside this two symbols %% to be font-style:italic; I've tried:

$find=array('`\^i`si');
$replace=array('font-style:italic;');   
$replaced = preg_replace($find,$replace,$string);

but it replaces the last ^i too also keep in mind that the string between %% can be also %^b ^i% so I can't condition that the string have to be %^i% Help!

Upvotes: 1

Views: 44

Answers (1)

Chin Leung
Chin Leung

Reputation: 14941

You could use this pattern instead:

$find = array('/%(.*?)(\^i)(.*?)%/', '/%(.*?)(\^b)(.*?)%/');
$replace = array('%$1font-style:italic;$3%', '%$1font-weight:bold;$3%');
$replaced = preg_replace('/%(.*)%/', '<span style="$1">', preg_replace($find, $replace, $string));

Upvotes: 0

Related Questions