Reputation: 15670
I have the following string:
$teststring = "
Flow Link Back Mdix
Port Type Duplex Speed Neg ctrl State Pressure Mode
fa1 100M-Copper -- -- -- -- Down -- --
fa2 100M-Copper -- -- -- -- Down -- --
fa3 100M-Copper -- -- -- -- Down -- --
fa4 100M-Copper -- -- -- -- Down -- --
fa5 100M-Copper -- -- -- -- Down -- --
fa6 100M-Copper -- -- -- -- Down -- --
fa7 100M-Copper -- -- -- -- Down -- --
fa8 100M-Copper -- -- -- -- Down -- --
gi1 1G-Combo-C -- -- -- -- Down -- --
gi2 1G-Combo-C Full 100 Enabled Off Up Disabled Off
Flow Link
Ch Type Duplex Speed Neg control State
Po1 -- -- -- -- -- Not Present Po2 -- -- -- -- -- Not Present Po3 -- -- -- -- -- Not Present Po4 -- -- -- -- -- Not Present Po5 -- -- -- -- -- Not Present Po6 -- -- -- -- -- Not Present Po7 -- -- -- -- -- Not Present Po8 -- -- -- -- -- Not Present " ;
I'm trying to parse each field. Here's the code I have so far:
$teststring = explode("<BR>", $teststring);
$vlandetailsArray = array();
foreach ($teststring as $vlandetails)
{
// port space type space duplex speed space neg
$pattern = '/([a-z0-9]*)(\s*)([a-z0-9\-]*)(\s*)[(Full)|(\-{2})](\s*)[(\-)+|(100)](\s*)[(--)*|(Enabled)](\s*)[(--)*|(Off)]/i';
if (preg_match($pattern, $vlandetails, $matches))
{
echo 'match 0: '.$matches[0].'<br>'; //0 index always returns all matches
}
This returns the following:
match 0: -------- ----
match 0: fa1 100M-Copper -- --
match 0: fa2 100M-Copper -- --
match 0: fa3 100M-Copper -- --
match 0: fa4 100M-Copper -- --
match 0: fa5 100M-Copper -- --
match 0: fa6 100M-Copper -- --
match 0: fa7 100M-Copper -- --
match 0: fa8 100M-Copper -- --
match 0: gi1 1G-Combo-C -- --
match 0: -------- ----
match 0: Po1 -- -- --
match 0: Po2 -- -- --
match 0: Po3 -- -- --
match 0: Po4 -- -- --
match 0: Po5 -- -- --
match 0: Po6 -- -- --
match 0: Po7 -- -- --
match 0: Po8 -- -- --
I don't understand why it's not picking up the line that looks like this:
gi2 1G-Combo-C Full 100 Enabled Off Up Disabled Off
Can you tell me what i'm missing / doing wrong?
FYI. I'm still playing around with my regex,so you'll notice that sometimes i use the pattern (-{2}) and other times -+ etc.
EDIT 1
I've modified the teststring. Previously, I had the following code to replace any CR LF with
.
$this->_data = str_replace(chr(10),"<BR>",$this->_data ,$count);//strip New line
$this->_data = str_replace(chr(13),'',$this->_data ,$count);//strip carriage return
I'm sorry, I overlooked these lines of code - have too many "tests" in my page. The test string you see now is the "raw" stuff. I just save everything to a file like so:
$fp = fopen('/var/www/lsm/application/logs/showinterfacesstatus.txt', 'w');
fwrite($fp, $this->_data);
fclose($fp);
where $this->_data contains the raw data. i opened this file and copied everything ... then pasted into my teststring variable.
Having said that, I have analyzed the file in a text editor and i can see that the only thing that's different between the original string and the modified string is that it has been stripped of all CRLFs. But in case it helps, I've removed this logic.
I've also included a screenshot of the unmodified data inside a text editor.
thanks.
Upvotes: 0
Views: 151
Reputation: 15670
here's the regex pattern that works:
$pattern = '/([a-z]{2}\d)(\s*)([0-9a-z-])(\s)([a-z-])(\s)([0-9-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-]*)/i';
Upvotes: 0
Reputation: 682
Try this one:
#[a-z]{2}\d\s[^\s]+ [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si
You must have case-insensitive mode enabled.
Other one, to capture the... inteface name?
#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si
Full code example:
<?php
$teststring = "
Flow Link Back Mdix
Port Type Duplex Speed Neg ctrl State Pressure Mode
-------- ------------ ------ ----- -------- ---- ----------- -------- -------
fa1 100M-Copper -- -- -- -- Down -- --
fa2 100M-Copper -- -- -- -- Down -- --
fa3 100M-Copper -- -- -- -- Down -- --
fa4 100M-Copper -- -- -- -- Down -- --
fa5 100M-Copper -- -- -- -- Down -- --
fa6 100M-Copper -- -- -- -- Down -- --
fa7 100M-Copper -- -- -- -- Down -- --
fa8 100M-Copper -- -- -- -- Down -- --
gi1 1G-Combo-C -- -- -- -- Down -- --
gi2 1G-Combo-C Full 100 Enabled Off Up Disabled Off
Flow Link
Ch Type Duplex Speed Neg control State
-------- ------- ------ ----- -------- ------- -----------
Po1 -- -- -- -- -- Not Present
Po2 -- -- -- -- -- Not Present
Po3 -- -- -- -- -- Not Present
Po4 -- -- -- -- -- Not Present
Po5 -- -- -- -- -- Not Present
Po6 -- -- -- -- -- Not Present
Po7 -- -- -- -- -- Not Present
Po8 -- -- -- -- -- Not Present";
$pattern = '#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si';
preg_match_all($pattern, $teststring, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
echo '<pre>';
var_dump($match);
echo '</pre>';
}
/* EOF */
Upvotes: 1