0

Giving a follow up it this post: How to extract img src, title and alt from html using php?

I would like to format the array, I have this example:

$articletxt = 'Hello! this is my picture <format type="table" name="My Picture" data="http://www.example.com/myimage.jpg" caption="I look tired" /> But I look better in real life <br><br> Hello! this is my picture <format type="youtube" name="Unlock Samsung S5310 Galaxy Pocket Neo by USB" data="piHMEZlAAmA" /> But I look better in real life';

preg_match_all('/<format[^>]+>/i',$articletxt, $formatbusquedas); 

$img = array();
foreach( $formatbusquedas[0] as $img_tag) {
    preg_match_all('/(type|name|data)=("[^"]*")/i',$img_tag, $img[$img_tag]);
}

echo '<pre>';
print_r($img);

It will print out this:

Array
(
    [<format tipo="table" name="My Picture" data="http://www.example.com/myimage.jpg" caption="I look tired" />] => Array
        (
            [0] => Array
                (
                    [0] => tipo="table"
                    [1] => name="My Picture"
                    [2] => data="http://www.example.com/myimage.jpg"
                )

            [1] => Array
                (
                    [0] => tipo
                    [1] => name
                    [2] => data
                )

            [2] => Array
                (
                    [0] => "table"
                    [1] => "My Picture"
                    [2] => "http://www.example.com/myimage.jpg"
                )

        )

    [<format tipo="youtube" name="Unlock Samsung S5310 Galaxy Pocket Neo by USB" data="piHMEZlAAmA" />] => Array
        (
            [0] => Array
                (
                    [0] => tipo="youtube"
                    [1] => name="Unlock Samsung S5310 Galaxy Pocket Neo by USB"
                    [2] => data="piHMEZlAAmA"
                )

            [1] => Array
                (
                    [0] => tipo
                    [1] => name
                    [2] => data
                )

            [2] => Array
                (
                    [0] => "youtube"
                    [1] => "Unlock Samsung S5310 Galaxy Pocket Neo by USB"
                    [2] => "piHMEZlAAmA"
                )

        )

)

But I need it like this:

Array
(
    [<format tipo="table" name="My Picture" data="http://www.example.com/myimage.jpg" caption="I look tired" />] => Array
        (
            [tipo] => table
            [name] => My Picture
            [data] => http://www.example.com/myimage.jpg

        )

    [<format tipo="youtube" name="Unlock Samsung S5310 Galaxy Pocket Neo by USB" data="piHMEZlAAmA" />] => Array
        (
            [tipo] => youtube
            [name] => Unlock Samsung S5310 Galaxy Pocket Neo by USB
            [data] => piHMEZlAAmA

        )

)

How can this be made?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

You can modify the $img array by applying array_combine to each of its elements to produce the output you want.

foreach ($img as &$value) {  // be sure to use a reference here.
    $value = array_combine($value[1], $value[2]);
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80