PHP: Convert <ul> <li> Tree HTML tag to an array

Error processing SSI file

Answers

  1. Reign

    • 2017/8/25

    Instead of messing with html you should submit the tree data in a more programmer-friendly format.

    $('#saveButton').click(function() {
        var treeData = $.tree.reference($('#sortableTree')).get(undefined, 'json');
        var tmp = serializeTree(treeData, 0);
        // now json-encode tmp and submit it
    });
    
    function serializeTree(nodes, parent)
    {
        var parents = {};
        var childOrder = []
        var childOrders = {};
        for(var i = 0; i < nodes.length; i++)
        {
            var node = nodes[i];
            var id = node.attributes.id.substr(5); // assuming the id looks like 'abcd-ID'
            parents[id] = parent;
            childOrder.push(id);
            if(node.children)
            {
                var tmp = serializeTree(node.children, id);
                for(var id in tmp[0])
                    parents[id] = tmp[0][id];
                for(var id in tmp[1])
                    childOrders[id] = tmp[1][id]
            }
        }
    
        childOrders[parent] = childOrder;
    
        return [parents, childOrders];
    }
    
  2. Jamir

    • 2018/10/8

    .

  3. Brown

    • 2021/6/20

    The htmlspecialchars_decode () function converts some predefined HTML entities to characters. HTML entities that will be decoded are: & becomes & (ampersand) " becomes " (double quote)

  4. Layne

    • 2015/2/16

    The iconv() function is an inbuilt function in PHP which is used to convert a string to requested character encoding. The iconv() is an 

  5. Emilio

    • 2018/12/12

    Regarding:

    My problem I'm having a hard time parsing the string HTML tag and save it in a PHP array.

    I suggest that you use an HTML parser, such as simplehtmldom. This will allow you to traverse over HTML DOM just the way you want.

    Here is a quick and dirty UL walking script:

    <?php
        require_once( "simplehtmldom/simple_html_dom.php" );
        $DOM = file_get_html( "test.htm" );
        $ARR = array( );
        function WalkUL( $ul, &$ar )
        {
            foreach( $ul->children as $li )
            {
                if ( $li->tag != "li" )
                {
                    continue;
                }
                $arar = array( );
                foreach( $li->children as $ulul )
                {
                    if ( $ulul->tag != "ul" )
                    {
                        continue;
                    }
                    WalkUL( $ulul, $arar );
                }
                $ar[ $li->find( "a", 0 )->plaintext ] = $arar;
            }
        }
        WalkUL( $DOM->find( "ul", 0 ), $ARR );
        print_r( $ARR );
    ?>
    

    Its output, not exactly as you wanted it but close:

    Array
    (
        [Folder 1] => Array
            (
                [Child 1.1] => Array
                    (
                    )
                [Folder 1.1] => Array
                    (
                        [Child 1.1.1] => Array
                            (
                            )
                    )
                [Folder 1.2] => Array
                    (
                        [Child 1.2.1] => Array
                            (
                            )
                        [Child 1.2.2] => Array
                            (
                            )
                    )
            )
        [Folder 2] => Array
            (
                [Child 2.1] => Array
                    (
                    )
                [Child 2.2] => Array
                    (
                    )
            )
        [Outer Child] => Array
            (
            )
    )
    
  6. Kelly

    • 2020/8/27

    Find Quality Results Related To Html Designer. Save Time & Get Answers on Search-HQ.com. Updated Today! Get The Information You Need Now.

  7. Raphael

    • 2017/8/23

    In multibyte characters, the number of leading 1 bits in the leftmost byte is the same as the number of bytes in the character. 16.12.4. See Also. Documentation on 

  8. Jedidiah

    • 2019/2/1

    Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.

  9. Henry

    • 2021/5/5

    Just for referecence: there's also a jQuery plugin for serializing a HTML DOM Tree of <ol> and <ul> lists: http://www.drakedata.com/serializetree/sampleTree.html - it converts a nested structure of <ol> and <ul> lists to a string which can be used as an associated php array.

  10. Benjamin

    • 2017/8/30

    UTF-8 encodes each character using one to four bytes. The first 128 characters of Unicode correspond one-to-one with ASCII, making valid ASCII text also valid 

  11. Gage

    • 2021/10/22

    Be sure to do SET NAMES utf8; (or equivalent, such as mysqli_set_charset ('utf8') ). Keep in mind that init_connect is not executed when you connect as root. Something like this will repair your data: UPDATE tbl SET col = CONVERT (BINARY ( CONVERT (col USING latin1)) USING utf8);

  12. Blake

    • 2020/5/24

    When you need to convert from HTML-ENTITIES, but your UTF-8 string is in UTF-8) - in this case passing string to mb_convert_encoding($string, 'UTF-8', 

  13. Alonzo

    • 2021/1/28

    If you have a UL that does not contain any href elements (e.g. just plain text nexted ul).. I've adjusted Salman's simplehtmldom code to work with that:

    $DOM = str_get_html( $catshtml );
    $ARR = array( );
    function WalkUL( $ul, &$ar )
    {
        foreach( $ul->children as $li )
        {
            if ( $li->tag != "li" )
            {
                continue;
            }
            $arar = array( );
            foreach( $li->children as $ulul )
            {
                if ( $ulul->tag != "ul" )
                {
                    continue;
                }
                WalkUL( $ulul, $arar );
            }
            $ar[ trim(preg_replace("#</?[a-z]+[^>]*>(.+</[a-z]+[^>]*>|)#is","",$li->innertext)) ] = $arar;
        }
    }
    WalkUL( $DOM->find( "ul", 0 ), $ARR );
    echo "<pre>"; print_r( $ARR );
    
  14. Garrett

    • 2017/7/26

    To convert your input to UTF-8, this tool splits the input data into individual graphemes (letters, numbers, emojis, and special Unicode symbols), then it extracts code points of all graphemes, and then turns them into UTF-8 byte values in the specified base. UTF-8 uses the following rules to encode the data.

  15. Calum

    • 2019/9/5

    If we know that the current encoding is ASCII, the 'iconv' function can be used to convert ASCII to UTF-8. The original string can be passed as a parameter to the iconv function to encode it to UTF-8.

  16. Yahir

    • 2015/10/12

    Convert ASCII TO UTF-8 Encoding in PHP? PHP Server Side Programming Programming. If we know that the current encoding is ASCII, the 'iconv' function can be used to convert ASCII to UTF-8. The original string can be passed as a parameter to the iconv function to encode it to UTF-8.

  17. Riley

    • 2018/5/26

    If you want, you can additionally declare the encoding in the generated HTML by using the <meta> tag: <meta http-equiv=Content-Type content="text/html; charset=UTF-8"> for HTML <=4.01. <meta charset="UTF-8"> for HTML5.

  18. Ryan

    • 2016/9/26

    For everybody who's looking for a function that is converting an iso-string to utf8 or an utf8-string to iso, here's your solution: public function encodeToUtf8 ($string) {. return mb_convert_encoding ($string, "UTF-8", mb_detect_encoding ($string, "UTF-8, ISO-8859-1, ISO-8859-15", true)); }

  19. Alessandro

    • 2017/12/15

    There is function json_encode() which is converted special characters to Unicode in PHP.

  20. Rinaldi

    • 2020/2/27

    convert_cyr_string — Convert from one Cyrillic character set to another Warning This function has been DEPRECATED as of PHP 7.4.0, and REMOVED as of PHP 8.0.0.

  21. Piras

    • 2021/9/8

    , none of them provide a proof of the concept.

  22. Soren

    • 2019/9/1

    The htmlspecialchars () function is used to converts special characters (e.g. & (ampersand), " (double quote), ' (single quote), < (less than), > (greater than)) to HTML entities (i.e. & (ampersand) becomes &amp, ' (single quote) becomes &#039, < (less than) becomes < (greater than) becomes >).

  23. Jacoby

    • 2019/5/29

    Just checking this out? Load sample data. Decode This PHP. Eval + gzinflate + Base64.

  24. Lorenzo

    • 2019/8/23

    PHP Version: 4.3.0+ Changelog: PHP 5.6 - Changed the default value for the character-set parameter to the value of the default charset (in configuration). PHP 5.4 - Changed the default value for the character-set parameter to UTF-8. PHP 5.4 - Added ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML. PHP 5.0 - Added support for multi-byte encodings

  25. Atlas

    • 2018/7/7

    iconv. (PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8). iconv — Convert string to requested 

  26. Felipe

    • 2019/2/15

    htmlspecialchars may be used: When there is no need to encode all characters which have their HTML equivalents. If you know that the page encoding match the text special symbols, why would you use htmlentities? htmlspecialchars is much straightforward, and produce less code to send to the client.

  27. Kyng

    • 2015/8/30

    Character Encoding and UTF-8 in PHP Show Summary UTF-8 uses 1-4 bytes per character--shows all Unicode characters while keeping a 

  28. Jason

    • 2017/3/17

    Display Unicode characters in PHP. Print Unicode characters PHP, I have read about the ord function, but it just returns the ASCII character code for a given character, hence the improper display of the ™ or While inserting the data into a MySQL database using the relevant collation (ex. utf8_general_ci for Unicode characters, i.e Hindi). Also in use the PHP predefined function utf8_decode to display the characters in HTML.

  29. Zayd

    • 2017/9/11

    $utf8string = html_entity_decode(preg_replace("/U\+([0-9A-F]{4})/", "&#x\\1;", $string), ENT_NOQUOTES, 'UTF-8'); is probably the simplest solution.

  30. Guerra

    • 2017/1/10

    I have my data in this format: U+597D or like this U+6211. I want to convert them to UTF-8 (original characters are 好 and 我). How can I do it?

  31. Ray

    • 2020/1/31

    htmlspecialchars_decode. (PHP 5 >= 5.1.0, PHP 7, PHP 8). htmlspecialchars_decode — Convert special HTML entities back to characters 

  32. Yousef

    • 2019/9/12

    Note that UTF-8 can represent many more characters than ISO-8859-1. Trying to convert a UTF-8 string that contains characters that can't be represented in ISO-8859-1 to ISO-8859-1 will garble your text and/or cause characters to go missing. Trying to convert text that is not encoded in UTF-8 using this function will most likely garble the text.

  33. Vincenzo

    • 2017/7/31

    When you need to convert from HTML-ENTITIES, but your UTF-8 string is partially the unicode conversion process convert the string to the target encoding

  34. Murati

    • 2015/4/18

    json_decode (string $json, bool|null $associative = null, int $depth = 512, int $flags = 0) : mixed Takes a JSON encoded string and converts it into a PHP variable.

  35. Parisi

    • 2017/11/13

    Get code examples like "convert ascii to utf-8 php" instantly right from your google search results with the Grepper Chrome Extension.

  36. Marlon

    • 2019/1/23

    For my last project I needed to convert several CSV files from Windows-1250 to UTF-8, and after several days of searching around I found a function that is partially solved my problem, but it still has not transformed all the characters.

  37. Marcellus

    • 2015/5/19

    UTF-8, ASCII compatible multi-byte 8-bit Unicode. htmlentities() - Convert all applicable characters to HTML entities I've checked these special entities:

  38. Callan

    • 2018/6/4

    As of PHP 7, you can use the Unicode codepoint escape syntax to do this. echo "\u {597D}"; outputs 好.

  39. Peter

    • 2015/3/30

    utf8_decode() - Converts a string with ISO-8859-1 characters encoded with UTF-​8 to GB2312 code to Unicode code which displayed the following function: <?

  40. Stephen

    • 2015/11/17

    What to Do to Convert Extended ASCII to UTF-8 PHP provides the utf8_encode()function. It recognizes the Extended ASCII character set to be ISO-8859-1 and converts the single-byte characters above code point 127 into UTF-8 multibyte characters. The conversion is a "mung" that cannot be done more than once (see the code snippet in "Pitfalls" below).

  41. Kevin

    • 2016/11/16

  42. Cade

    • 2018/7/28

    PHP Decoder Very useful for webmasters trying to identify what a specific code is doing. (from WordPress themes/plugins or Joomla templates)

  43. Landry

    • 2015/1/3

    htmlspecialchars. (PHP 4, PHP 5, PHP 7). htmlspecialchars — Convert special characters to HTML entities. Description.

  44. Chavez

    • 2017/7/31

    this led to confusion when i tried to convert this euro to UTF-8 via mb_convert_encoding() IE displays UTF-8 correctly- and because PHP correctly converted #128 into a box in UTF-8, IE would show a box. so all i saw was mb_convert_encoding() converting a euro symbol into a box. It took me a long time to figure out what was going on.

Comments are closed.

More Posts