regex - Convert String to Object PHP

53

I am new to PHP. I know there are lot of answers for this. But I can't find a suitable solution for my case. I have a string which looks like this:

lo:
00:00:00:00:00:00
enp0s25:
00:21:cc:d4:2a:23
wlp3s0:
84:3a:4b:03:3c:3a
wwp0s20u4:
7a:e3:2a:de:66:09
br-a02a0801833f:
02:42:75:b4:8b:58
docker0:
02:42:cb:f0:5f:0d

I want to convert it to a JSON object which needs to be like this.

{
 "lo":"00:00:00:00:00:00",
 "enp0s25":"00:21:cc:d4:2a:23",
 "wlp3s0":"84:3a:4b:03:3c:3a",
 "wwp0s20u4":"7a:e3:2a:de:66:09",
 "br-a02a0801833f":"02:42:75:b4:8b:58"
 "docker0":"02:42:cb:f0:5f:0d"
}

Help me with some solutions. Here's the Code:

<?php
        $mac = shell_exec("ip link | awk '{print $2}'");
        echo($mac);
        print_r("type:  ");
        print_r(gettype($mac));
        $check = json_decode($mac);
        print_r($check);     
?>

People are also looking for solutions to the problem: What is the difference between Plaintext and Innertext of an element in Simple HTML dom parser in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.