1 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以嘗試將 preg_replace() 與模式一起使用來(lái)執(zhí)行此操作:
<?php
// your example text
$text = "^b(This sentence becomes bold), and ^i(this becomes italic).\nThen this becomes ^up(uppercase).";
// array of patterns
$patterns = [];
$patterns[0] = "/\^b\((.*?)\)/";
$patterns[1] = "/\^i\((.*?)\)/";
$patterns[2] = "/\^up\((.*?)\)/";
// array of replacements
$replacements = [];
$replacements[0] = '<b>${1}</b>';
$replacements[1] = '<i>${1}</i>';
$replacements[2] = '<span style="text-transform:uppercase;">${1}</span>'; // or use something better here
// process the text
$formattedText = preg_replace($patterns, $replacements, $text);
// see the result
echo $formattedText;
?>
如果你把這個(gè)邏輯寫在一個(gè)輔助函數(shù)中會(huì)好得多,這樣你以后可以在不同的地方更容易地使用它。
- 1 回答
- 0 關(guān)注
- 204 瀏覽
添加回答
舉報(bào)