我从用户收到字符串,并将其传递给preg_match以匹配模式(作为输出变量)
$regex = '/(?<L_Hostname>\S+).+?Software,\s(?<L_Platform>\S+).+?\((?<L_IOS_Type>.+?)\),\sVersion\s(?<L_IOS_Versioon>.+?),.+?#\s(?<CDP_R_Device_ID>.+)\sIP address: (?<CDP_R_IP_Address>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})\sPlatform:\s(?<CDP_R_Platform>.+?),\sCapabilities:\s(?<CDP_R_Capabilities>.+?)\s\Interface:\s(?<CDP_L_Interface>.+?),\sPort ID \(outgoing port\):\s(?<CDP_R_Interface>.*?)\sVersion.+?Version\s(?<CDP_R_Version>.+?)/i';
if(preg_match_all($regex,$output, $matches))
{
$result->regex = $regex;
$result->variable = serialize($matches);
$result->save();
}
但上面的代码不起作用,结果是空数组。当我打印输出并复制以使其工作
时$regex = '/(?<L_Hostname>\S+).+?Software,\s(?<L_Platform>\S+).+?\((?<L_IOS_Type>.+?)\),\sVersion\s(?<L_IOS_Versioon>.+?),.+?#\s(?<CDP_R_Device_ID>.+)\sIP address: (?<CDP_R_IP_Address>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})\sPlatform:\s(?<CDP_R_Platform>.+?),\sCapabilities:\s(?<CDP_R_Capabilities>.+?)\s\Interface:\s(?<CDP_L_Interface>.+?),\sPort ID \(outgoing port\):\s(?<CDP_R_Interface>.*?)\sVersion.+?Version\s(?<CDP_R_Version>.+?)/i';
$output = ' nano- nano-# Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.2(4)M4, RELEASE SOFTWARE (fc2) nano-# apk-sw3750.ali IP address: 192.168.1.240 Platform: cisco WS-C3750-24TS, Capabilities: Switch IGMP Interface: FastEthernet1, Port ID (outgoing port): FastEthernet1/0/3 Version : Cisco IOS Software, C3750 Software (C3750-ADVIPSERVICESK9-M), Version 12.2(46)SE, RELEASE SOFTWARE (fc2) nano-# ';
if(preg_match_all($regex,$output, $matches))
{
$result->regex = $regex;
$result->variable = serialize($matches);
$result->save();
}