phphtmlarrayshtml-parsingassociative-array

Parse HTML content into an associative array?


I have a string like this:

$str = "<div>
            <b>
                <label>Company-Name : Anything</label>
            </b>
        </div>
        <div>
            <b>
                <label>First-Name : Alex</label>
            </b>
        </div>
        <div>
            <b>
                <label>Cell-Phone : 035123913</label>
            </b>
        </div>";

Now I want this array:

$arr = array ("Company-Name"=>"Anything",
              "First-Name"=>"Alex",
              "Cell-Phone"=>"035123913");

How can I do that?


As far as I realized by searching, there is a function which removes all html tags:

strip_tags($str);

I think the above function is a clue. Also there is two characters which are constant: \n and :. I think we can use them to explode the result. (however I'm not sure, maybe I'm wrong)


Solution

  • I like the strip_tags approach, but here is a regex take:

    $str = "<div>
                <b>
                    <label>Company-Name : Anything</label>
                </b>
            </div>
            <div>
                <b>
                    <label>First-Name : Alex</label>
                </b>
            </div>
            <div>
                <b>
                    <label>Cell-Phone : 035123913</label>
                </b>
            </div>";
    
    preg_match_all('/<label>(.*)<\/label>/', $str, $matches);
    foreach($matches[1] as $match) {
        $parts = explode(':', $match);
        $results[trim($parts[0])] = trim($parts[1]);
    }
    var_dump($results);
    

    Output:

    array (size=3)
      'Company-Name' => string 'Anything' (length=8)
      'First-Name' => string 'Alex' (length=4)
      'Cell-Phone' => string '035123913' (length=9)