Encoding issue, coverting & to & for html using php

后端 未结 4 808
無奈伤痛
無奈伤痛 2021-02-19 07:37

I have a url in html:


I need to turn it into a string exactly as:



        
相关标签:
4条回答
  • 2021-02-19 07:47

    use html_entity_decode():

    $newUrl = html_entity_decode('<a href="index.php?q=event&amp;id=56&amp;date=128">');
    echo $newUrl; // prints <a href="index.php?q=event&id=56&date=128">
    
    0 讨论(0)
  • 2021-02-19 08:01

    Use htmlspecialchars_decode. Example straight from the PHP documentation page:

    $str = '<p>this -&gt; &quot;</p>';
    echo htmlspecialchars_decode($str); // <p>this -> "</p>
    
    0 讨论(0)
  • 2021-02-19 08:03

    after string go through TinyMCE only this code help me

    $string = iconv('UTF-8','cp1251',$string);
    $string = str_replace(chr(160), chr(32), $string);
    $string = iconv('cp1251','UTF-8',$string);
    
    0 讨论(0)
  • 2021-02-19 08:04

    There is no built in PHP function that will take an entity such as &amp; and turn it into a double &. Just in case there is any confusion, the html entity for & is actually &amp;, not amp;, so running any built in parser on your example will return the following:

    <a href="index.php?q=event&id=56&date=128">
    

    and not

    <a href="index.php?q=event&&id=56&&date=128">
    

    In order to get the double & version, you will need to use a regular expression.

    Alternatively, if you in fact want the single & version, you have two possibilities.

    1. If you just wish to convert &amp; &quot; &#039; &lt; &gt; then you should use htmlspecialchars_decode. This would be sufficient for the example you give.
    2. If you wish to convert any string of the format &TEXT; then you should use html-entity-decode.

    I suspect that htmlspecialchars_decode will be faster than html_entity_decode so if it covers all the entities you wish to convert, you should use that.

    0 讨论(0)
提交回复
热议问题