2

テキスト内のすべての HTML マークアップを BB コードに置き換える必要があり、インラインの CSS スタイル要素を置き換えるのに苦労しています。

HTML:

<span style="font-family: arial;">Arial</span>
<span style="font-size: 7px;">small text</span>
<span style="color: skyblue;">Blue Text</span>
<div align="center">centered text</div>

次のものに置き換えられる必要があります: BB コード:

[FONT=arial]Arial[/FONT]
[SIZE=7]small text[/SIZE]
[COLOR=skyblue]Blue Text[/COLOR]
[CENTER]centered text[/CENTER]

一方、フォント ファミリ、カラー コード、およびサイズの値は異なる場合があります。

非稼働開始点:

$text = preg_replace('#<span style="color: (.*?);">(.*?)</span>#siU', '[color=$1]$2[/color]', $text);
4

2 に答える 2

1

どうですか

$text = preg_replace('/<span style="color: (.*?);">(.*?)<\/span>/siU', '[color=$1]$2[/color]', $text);

をに置き換えただけ#/、うまく機能しています。
他の代替品については、次のとおりです。

/<span style="font-size: (.*?)px;">(.*?)<\/span>/
[size=$1]$2[/size]

/<span style="font-family: (.*?);">(.*?)<\/span>/
[font=$1]$2[/font]

/<div align="(.*?)">(.*?)<\/div>/
[$1]$2[/$1]

更新
それをいじっ た:phpfiddle.org/main/code/zqu-bgs
(終了/タグのエスケープ終了)

于 2012-11-23T09:19:51.970 に答える
0

これを試して:

$str1 = '<span style="font-family: arial;">Arial</span>'; 
$str2 = '<span style="font-size: 7px;">small text</span>';
$str3 = '<span style="color: skyblue;">Blue Text</span>';

$reg_exp=  '/<span\s*style=\"\s*(.*?):\s*(.*?)\s*;\s*\">\s*(.*?)\s*<\/span>/';
$reg_exp_rep = '[$1=$2]$3[/$1]';


$text=preg_replace($reg_exp,$reg_exp_rep, $str1);
echo htmlspecialchars($str1).' <=> '.$text.'<br />';
$text=preg_replace($reg_exp,$reg_exp_rep, $str2);
echo htmlspecialchars($str2).' <=> '.$text.'<br />';
$text=preg_replace($reg_exp,$reg_exp_rep, $str3);
echo htmlspecialchars($str3).' <=> '.$text.'<br />';

結果:

<span style="font-family: arial;">Arial</span> <=> [font-family=arial]Arial[/font-family]
<span style="font-size: 7px;">small text</span> <=> [font-size=7px]small text[/font-size]
<span style="color: skyblue;">Blue Text</span> <=> [color=skyblue]Blue Text[/color]

これは、一般的な span タグ用です。より異なるタグ認識またはより具体的なものが必要な場合は、それを定義する必要があります。

--

編集:より具体的な、あなたが求めたもの。

unset($str1,$str2,$str3,$str4,$str5,$str6,$str7,$reg_exp);

$str1 = '
    <span style="font-family: arial;">Arial</span> 
    <span style="font-size: 7px;">small text</span>
    <span style="color: skyblue;">Blue Text</span>
    <div align="center">centered text</div>';

$reg_exp[0] =  '/<span\s*style=\"\s*(.*?):\s*(.*?)\s*;\s*\">\s*(.*?)\s*<\/span>/';
$reg_exp_rep[0] = '[$1=$2]$3[/$1]';
$reg_exp[1] =  '/\[font-family(.*?)\/font-family\]/';
$reg_exp_rep[1] = '[FONT$1/FONT]';
$reg_exp[2]=  '/\[font-size(.*?)\/font-size\]/';
$reg_exp_rep[2] = '[SIZE$1/SIZE]';
$reg_exp[3] =  '/\[color(.*?)\/color\]/';
$reg_exp_rep[3] = '[COLOR$1/COLOR]';
$reg_exp[4] =  '/<div\s*align=\"\s*center\s*"\s*\>\s*(.*?)\s*<\/div>/';
$reg_exp_rep[4]= '[CENTER]$1[/CENTER]';

$text=$str1;
foreach ($reg_exp as $ii => $exp) {
    $text=preg_replace($exp,$reg_exp_rep[$ii], $text);
}
    echo htmlspecialchars($str1).' <=> '.$text.'<br />';

結果:

<span style="font-family: arial;">Arial</span> <span style="font-size: 7px;">small text</span> <span style="color: skyblue;">Blue Text</span> <div align="center">centered text</div> <=> 
[FONT=arial]Arial[/FONT] [SIZE=7px]small text[/SIZE] [COLOR=skyblue]Blue Text[/COLOR] [CENTER]centered text[/CENTER]
于 2012-11-23T09:30:00.520 に答える