[英]Yii2 form field modify value with regex
我在 yii2 中有一個編輯表單(WYSIWYG 編輯器作為文本輸入),默認值為一個表(該表來自我的數據庫),具有多個這樣的樣式標簽
<table border="0" cellpadding="0" cellspacing="0" style="width:694px">
<tbody>
<tr>
<td style="height:15.75pt; width:128pt">Dimension</td>
<td style="width:11pt">:</td>
<td style="width:381pt">31 mm x 8 mm</td>
</tr>
<tr>
<td style="height:15.75pt">Band width </td>
<td>:</td>
<td>18 mm</td>
</tr>
<tr>
<td style="height:15.75pt">Case </td>
<td>:</td>
<td>Stainless steel | Ceramic bezel</td>
</tr>
<tr>
<td style="height:15.75pt">Finishing</td>
<td>:</td>
<td>Stainless steel</td>
</tr>
<tr>
<td style="height:15.75pt">Glass</td>
<td>:</td>
<td>Scratch resistant mineral glass</td>
</tr>
<tr>
<td style="height:15.75pt">Strap</td>
<td>:</td>
<td>Leather strap</td>
</tr>
<tr>
<td style="height:15.75pt">Buckle</td>
<td>:</td>
<td>Stainless steel</td>
</tr>
<tr>
<td style="height:15.75pt">Movement</td>
<td>:</td>
<td>Quartz 3 hand movement</td>
</tr>
<tr>
<td style="height:15.75pt">Function</td>
<td>:</td>
<td>Hour | Minute | Second</td>
</tr>
<tr>
<td style="height:15.75pt">Water resistant</td>
<td>:</td>
<td>3 ATM</td>
</tr>
</tbody>
我想在我的所見即所得編輯器中將表格呈現為默認值之前刪除該表格中的每個樣式標簽,我該如何實現? 這是我的代碼
<div class="col-sm-10">
<?= $form->field($productDetail, 'spesification')->textarea(array("rows" => 10, "cols" => 80))->label(false) ?>
</div>
我知道我必須使用preg_replace
來刪除所有樣式標簽,如下面的代碼,但我不確定如何在表單字段中實現這一點。 那么誰能告訴我如何在我的表單字段上使用下面的代碼,或者有沒有其他解決方案? 謝謝你
$productDetail->spesification = preg_replace('/ style=("|\')(.*?)("|\')/', '', $_POST['ProductDetail']['spesification']);
首先,您嘗試使用的正則表達式會留下style=
字符串的一部分,這很糟糕。 您應該使用preg_replace('/(\sstyle=["\'].*["\'])/gU', '', $input)
代替。
其次,您嘗試替換一些 POST 數據,而不是生成的 HTML 代碼。 您應該在上面的 function 中使用$form->field()
返回值作為$input
,如下所示:
<div class="col-sm-10">
<?php
$field = $form
->field($productDetail, 'spesification')
->textarea(array("rows" => 10, "cols" => 80))
->label(false);
$clearField = preg_replace('/(\sstyle=["\'].*["\'])/gU', '', $field);
echo $clearField;
?>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.