htmlspecialchars

php htmlentities和htmlspecialchars 的区别

拟墨画扇 提交于 2019-12-10 09:11:03
很多人都以为htmlentities跟htmlspecialchars的功能是一样的,都是格式化html代码的,我以前也曾这么认为,但是今天我发现并不是这样的。 The translations performed are: 代码如下: '&' (ampersand) becomes '&' '"' (double quote) becomes '"' when ENT_NOQUOTES is not set. ''' (single quote) becomes ''' only when ENT_QUOTES is set. '<' (less than) becomes '<' '>' (greater than) becomes '>' htmlspecialchars 只转化上面这几个html代码,而 htmlentities 却会转化所有的html代码,连同里面的它无法识别的中文字符也给转化了。 我们可以拿一个简单的例子来做比较: 代码如下: $str='<a href="test.html">测试页面</a>'; echo htmlentities($str); // <a href="test.html">²âÊÔÒ³Ãæ</a> $str='<a href="test.html">测试页面</a>'; echo htmlspecialchars($str); //

Convert HTML entities and special characters to UTF8 text in PHP

只谈情不闲聊 提交于 2019-12-08 05:57:43
问题 There are a lot of questions and documentation about converting HTML entities and special characters to UTF8 text in PHP. And also there is the PHP documentation itself, such as this htmlspecialchars_decode() and this html_entity_decode(). However, I could not find any function/solution that clearly describes how to convert any HTML characters and special entities to UTF-8 text. All of them state something like "if you want to do this, then do that", etc. But no solution ever states " to have

htmlspecialchars & ENT_QUOTES not working?

做~自己de王妃 提交于 2019-12-07 04:51:31
问题 Basically on displaying data from MySQL database I have a htmlspecialchars() function below that should convert single and double quotes to their safe entity(s). The problem I'm having is on viewing source code, it is only converting < > & when I also need it to convert single and double quotes. //sanitize data from db before displaying on webpage function htmlsan($htmlsanitize){ return $htmlsanitize = htmlspecialchars($htmlsanitize, ENT_QUOTES, 'UTF-8'); } Then when I want to use for example

htmlspecialchars & ENT_QUOTES not working?

非 Y 不嫁゛ 提交于 2019-12-05 09:49:21
Basically on displaying data from MySQL database I have a htmlspecialchars() function below that should convert single and double quotes to their safe entity(s). The problem I'm having is on viewing source code, it is only converting < > & when I also need it to convert single and double quotes. //sanitize data from db before displaying on webpage function htmlsan($htmlsanitize){ return $htmlsanitize = htmlspecialchars($htmlsanitize, ENT_QUOTES, 'UTF-8'); } Then when I want to use for example I do: htmlsan($row['comment']); Can someone tell me why it's not converting single and double quotes?

htmlspecialchars、strip_tags和addslashes的区别

筅森魡賤 提交于 2019-12-05 08:48:26
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。 预定义的字符是: & (和号) 成为 & " (双引号) 成为 " ' (单引号) 成为 ' < (小于) 成为 < > (大于) 成为 > addslashes() 函数在指定的预定义字符前添加反斜杠。 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL <?php $str = "Who's John Adams?"; echo $str . " This is not safe in a database query.<br />"; echo[object Object]de> . " This is safe in a database query."; ?> Who's John Adams? This is not safe in a database query. Who\'s John Adams? This is safe in a database query. strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签。 即不带任何的标记,转换成字符串。 <?php echo strip_tags("Hello <b><i>world!</i></b>",[object Object]de>); ?> strip_tags

Twig - use quotation mark as separator for join filter

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 18:21:29
I pass my template an array of strings which I would like to convert to a jaavascript array: Controller file (php): $myVar = array('a','b','c'); Desired html: var myVar = ["a","b","c"]; I try the following code (twig): var myVar = ["{{ myVar | join('","') }}"]; But the twig generator converts the quotation marks to html entities and this is the result: var myVar = ["a&quot;,&quot;b&quot;,&quot;c"]; Some idea? You need to apply the raw filter: var myVar = ["{{ myVar | join('","') | raw }}"]; 来源: https://stackoverflow.com/questions/11557010/twig-use-quotation-mark-as-separator-for-join-filter

php XML DOM translates special chars to &#xYY;

邮差的信 提交于 2019-12-04 12:11:11
I send this with AJAX POST: <li><ul class "zone zCentral ui-sortable"><li><ul class="region rCol3 ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span> <span>video: 'Mundo.Hoy': ¿Dónde habré olvidado... mi memoria?</span></div></li></ul></li></ul></li> I do this to create XML: header('Content-type: text/html; charset=utf-8'); if(isset($_POST) && isset($_POST['data'])) { $data = '<ul id="zone_container" class="ui-sortable">'; $data .= $_POST['data']; $data .= '</ul>'; $dom = new DOMDocument('1.0', 'utf-8'); $dom->loadXML($data); echo $dom->saveXML(); exit(); } and i

Is it good to use htmlspecialchars() before Inserting into MySQL?

好久不见. 提交于 2019-12-04 02:29:34
I am a little confused on this. I have been reading about htmlspecialchars() and I am planning to use this for the textareas POST to prevent XSS attack. I understand that usually htmlspecialchars() are used to generate the HTML output that is sent to the browser. But what I am not sure is: 1) Is it a safe practice to use htmlspecialchars() to the user input data before I insert it into MySQL? I am already using PDO prepared statement with parameterized values to prevent SQL Injection. 2) Or, I really dont need to worry about using htmlspecialchars() to inserted values (provided they are

Sanitizing PHP/SQL $_POST, $_GET, etc…?

半腔热情 提交于 2019-12-04 01:52:40
问题 Ok, this subject is a hotbed I understand that. I also understand that this situation is dependent on what you are using as code. I have three situations that need to be resolved. I have a form in where we need to allow people to make comments and statements that use commas, tildes, etc... but still remain safe from attacks. I have people entering in dates like this: 10/13/11 mm/dd/yy in English, can this be sanitized? How do I understand how to use htmlspecialchars() , htmlentities() and

is it better to escape/encode the user input before storing it to database or to store it as it is in database and escape it while retrieving?

一笑奈何 提交于 2019-12-01 18:30:45
I am using htmlspecialchars() function to prevent XSS attacks. I have doubt regarding what is the better method to store the data in database from following. Method 1 : Store the user input values after applying htmlspecialchars() function. Using this it user input "<script>" will become "<script>" . Method 2 : Store the user input as it is and apply htmlspecialchars() method while retrieving the data and displaying it on the page. The reason for my doubt is that I believe using method 1 there will be overhead on database, while using method 2 data need to be converted again and again when