Çift tırnak ve Input Sorunu

3 Cevap php

i Aşağıdaki kodu vardır:

 <input type="text" value="<?php echo $_GET['msg']; ?>">

This input is automatically filled with the name that is writen in the previous page. So, if the user wrote : i like "apples" and banana The input will be broken because it will close the tag after the double quotes. I know i can avoid that by html entiting the value, but i don't want this, is there another solution or is there an <<< EOD in html ?

Teşekkürler

3 Cevap

htmlentities() / htmlspecialchars() Bunun için standart bir yoludur. Bunu kullanmak gerekir.

Her zaman e-posta ile gönderebilirsiniz önce varlıkları çözmek, ya da html_entity_decode() kullanarak onlarla birlikte başka bir şey yapabiliriz.

HTML için çıkış kaçmak için, htmlspecialchars işlevini kullanmanız gerekir:

<input type="text" value="<?php echo htmlspecialchars($_GET['msg']); ?>">

Not: Eğer charset ISO-8859-1 kullanmıyorsanız eğer, bazı additionnal parametreleri eklemek gerekebilir; Örneğin, UTF-8 ile:

<input type="text" value="<?php echo htmlspecialchars($_GET['msg'], ENT_COMPAT, 'UTF-8'); ?>">

Bir fonksiyon ya da başka bir sorun çeşit neden olur.

Ben işareti tutmak için aşağıdaki ile geldi:

<input type="text" value="<?php echo parseString($_GET['msg']); ?>">
<?php
function parseString($str) {
    $result=str_replace('"','&quot;',$str);
    $result=str_replace("'","&#39;",$result);
    return $result;
}