How to replace % with html tag in PHP -


i have following code in php:

$fullstring = "a %sample% word go %here%"; 

and want replace % html tag (<span style='color:red'> , </span>). want after replacing:

$fullstring ="a <span style='color:red'>sample</span> word go <span style='color:red'>here</span>"; 

what should accomplish result using php function str_replace, preg_replace etc.? thanks.

you do....

$string = "a %sample% word go %here%"; echo preg_replace('~%(.*?)%~', '<span style="color:red">$1</span>', $string); 

that says replace between first % , next occurring % sign spans. () groups inside percents $1.

output:

a <span style="color:red">sample</span> word go <span style="color:red">here</span> 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -