1

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)

5 Answers 5

2

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)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a double explode.

$str = explode(PHP_EOL, $str);
$arr = array();
foreach ($str as $val) {
  $arr[] = explode(":", $val);
}

You might need to trim() in all the cases I guess.

10 Comments

I think you have to remove all html tags. Here is the output of your code 3v4l.org/dqrCg
Yes I can use it, thank you, I just mentioned to that point because I wanted when other users use your code, then see a desired result. anyway ok thanks again. +1
@stack Once you get it right, feel free to edit my answer bro! :) You have all rights! :)
To be honest, that's scare for me to edit your answer, who has 80K rep :) .
@stack I just gave you the full freedom. Doesn't mean if I have 80k Rep, I am a master! And those with less are not! :)
|
1

A shorter way to obtain your desired result (without stripping tags), is to use a regular expression:

preg_match_all( "{<label>([^:]+) +: +([^<]+)</label>}", $str, $matches );

By this way, in $matches array, you will obtain this:

Array
(
    [0] => Array
        (
            [0] => <label>Company-Name : Anything</label>
            [1] => <label>First-Name : Alex</label>
            [2] => <label>Cell-Phone : 035123913</label>
        )

    [1] => Array
        (
            [0] => Company-Name
            [1] => First-Name
            [2] => Cell-Phone
        )

    [2] => Array
        (
            [0] => Anything
            [1] => Alex
            [2] => 035123913
        )

)

2 Comments

Ha ha... Nice one, we need to then map it with each other.
@PraveenKumar oh, yes
1

This solves the problem, built upon the previous answer by @Praveen Kumar

$str = explode(PHP_EOL, $str);
$arr = [];

foreach ($str as $val) {
    $pos=strpos($val, ":");
    if ($pos !== false)
    {
        $arr[] = explode(":", strip_tags($val));
    }

}

foreach($arr as $key=>$value){
   $final_array[trim($value[0])] = trim($value[1]);
}

var_dump($final_array);

Output

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

Comments

0

When your task involves parsing HTML, use a legitimate HTML parser as much as possible to avoid unintended matches and breakages.

Once the label nodes are isolated, split on the delimiter and declare the associative elements in the result array from the two halves.

Code: (Demo)

$html = <<<HTML
<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>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html);
$result = [];
foreach ($dom->getElementsByTagName('label') as $label) {
    [$k, $result[$k]] = explode(' : ', $label->nodeValue, 2);
}
var_export($result);

Output:

array (
  'Company-Name' => 'Anything',
  'First-Name' => 'Alex',
  'Cell-Phone' => '035123913',
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.