0

textarea後でデータベースに送り返すために使用できるように、それぞれに名前を付けようとしています。このコードを使用すると、いくつかの奇妙な結果が得られましたstr_replace

コードは次のとおりです。

$description2 = mysql_result($product, 0, 'productDescription2');
$totalteknisk = preg_match_all('/x(a|b|d|e)/', $description2, $matches);
$searchArray = array('xa', 'xb', 'xc', 'xd', 'xe');

if ($description2 !=""){
for($z=1;$z <= $totalteknisk; $z++){
$xa = '<textarea name="'. $z .'" style="background-color:#FFFFFF;resize: none; height: 20px; width: 200px;">';
$z++;
$xb ='</textarea><textarea name="'. $z .'" style="background-color:#FFFFFF;resize: none; height: 20px; width: 200px;">';
$z++;
$xc = '</textarea><br>';
$xd = '<textarea name="'. $z .'" style="background-color:#EAF2D3;resize: none; height: 20px; width: 200px;">';
$z++;
$xe = '</textarea><textarea name="'. $z .'" style="background-color:#EAF2D3;resize: none; height: 20px; width: 200px;">';
$replaceArray = array($xa, $xb, $xc, $xd, $xe);
$teknisk .=  str_replace($searchArray, $replaceArray, $description2);   
}                               
}

データベースからの文字列の例xa1xb2xcxd3xe4xcxa5xb6xc(説明 2)

ご覧のとおり、すべてをループして値 1 ~ を指定しようとしています$totalteknisk

これを機能させる方法についての提案をお待ちしています。

4

1 に答える 1

0

preg_replace_callback(...)を使用して、少し異なる方法で解決しました。

$description2 = 'xa1xb2xcxd3xe4xcxa5xb6xc';

$html = preg_replace_callback('/x(?:a|b|c|d|e)/', function($match) {
    static $count;

    $count++;

    switch($match[0]) {
        case 'xa':
            return "<textarea name=\"$count\" style=\"background-color:#FFFFFF;resize: none; height: 20px; width: 200px;\">";
        case 'xb':
            return "</textarea><textarea name=\"$count\" style=\"background-color:#FFFFFF;resize: none; height: 20px; width: 200px;\">";
        case 'xc':
            return "</textarea><br />";
        case 'xd':
            return "<textarea name=\"$count\" style=\"background-color:#EAF2D3;resize: none; height: 20px; width: 200px;\">";
        case 'xe':
            return "</textarea><textarea name=\"$count\" style=\"background-color:#EAF2D3;resize: none; height: 20px; width: 200px;\">";
    }
}, $description2);

$teknisk .= $html;

また、textareasの「name」属性に数字だけを使用しないことをお勧めします。のような別の名前を使用することを検討してから、次のようfields[$count]に参照する必要があります。$_POST['fields'] ...

于 2012-06-04T14:18:22.953 に答える