【问题标题】:Yii2 form field modify value with regexYii2 表单字段使用正则表达式修改值
【发布时间】:2021-05-02 03:38:32
【问题描述】:

我在 yii2 中有一个编辑表单(所见即所得编辑器作为文本输入),默认值是一个带有多个样式标签的表格(表格来自我的数据库)

<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&nbsp;</td>
        <td>:</td>
        <td>18 mm</td>
    </tr>
    <tr>
        <td style="height:15.75pt">Case&nbsp;</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']);

【问题讨论】:

    标签: php html yii2


    【解决方案1】:

    首先,您尝试使用的正则表达式会留下style= 字符串的一部分,这很糟糕。你应该改用preg_replace('/(\sstyle=["\'].*["\'])/gU', '', $input)

    其次,您尝试替换一些 POST 数据,而不是生成的 HTML 代码。您应该在上面的函数中使用$form-&gt;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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      相关资源
      最近更新 更多