Vipul Jethva
Vipul Jethva

Reputation: 675

PHP to extract attribute and value from the given string

Here is my response. I am developing something like b bcode

[quote="abc" post_id='5' time='1728016909' user_id='117']
Around & Business 78
[/quote]

And I need output(Dynamic):

array(
quote=> "abc",
post_id=> '5'
time=> '1728016909'
user_id => '117'
);

I tried following code:

$string = "[quote='abc' post_id='5' time='1728016909' user_id='117']Around & Business 78[/quote]";

    $dom = new DOMDocument;
                    $dom->loadHTML($string); // [quote="abc" ......
                    $attr = array();
                    var_dump($dom->getElementsByTagName('quote'));
                    foreach ($dom->getElementsByTagName('quote') as $tag) {
                        foreach ($tag->attributes as $attribName => $attribNodeVal){
                            $attr[$attribName]=$tag->getAttribute($attribName);
                        }
                    }
                    echo "<pre>"; print_r($attr);

Please correct easy solution!

Upvotes: 0

Views: 29

Answers (0)

Related Questions