Home > other >  How to filter specific string part from array in php?
How to filter specific string part from array in php?

Time:07-12

// if (strpos($output, 'status: up') !== false) {
//     print("true!");
// }

$output - prints below array, How to filter (status: up) lines from this array ? I tried above code but I'm unable to get complete lines.

QLogic BCM57810 10 Gigabit Ethernet (NDIS VBD Client) status: up speed: 0 enabled: true,
Microsoft Kernel Debug Network Adapter status: down speed: 0 enabled: false,
WAN Miniport (IKEv2) status: down speed: 0 enabled: false,
WAN Miniport (L2TP) status: down speed: 0 enabled: false,
WAN Miniport (PPTP) status: down speed: 0 enabled: false,
WAN Miniport (PPPOE) status: down speed: 0 enabled: false,
WAN Miniport (GRE) status: down speed: 0 enabled: false

CodePudding user response:

<?php
// Assuming $output as the array name.
// Assuming ',' is the delimiter, based on your example.

function conditionFunction($line)
{
    return (strpos($var, 'status: up') !== false)?true:false;
}

$statusLines = explode(",", $output); // Separating the lines into an array object.
print_r(array_filter($statusLines, "conditionFunction"));

?>

Obs. If this ternary condition doesn't work directly with the return keyword, try to store in a variable and then return.

  • Related