php - How to make a regex to recognise a specific pattern of words -
i'm poor in regex, reason left no choice use it.
i'm trying extract list of "port number" , respective "ip address" webpage's table. , because dynamic webpage using ajax , php stuff generate dynamic content, table elements doesn't have id or class or unique things. had eliminates /t, /r , /n
using str_replace
, whole content contains words , spaces.
here example of port , ip addr:
port - fa0/0, gi1/0/2.100, ethernet01, gigaether-01 (contains upper , lower case, dot, dash, slash , numbers, , shouldn't more 16 characters, no spaces)
ip adrr - 123.123.123.123, 1.1.12.12, 123.12.1.1 (no difference common ip addr)
but fortunately, "port" , "ip address" followed either port image or ip image., like
...<img border='0' src='images/port.png' width='18' heigh='18'>fa0/0</td>... or ...<img border='0' src='images/ip.png' width='18' heigh='18'>1.1.1.1</td>...
i believe there no spaces between port/ip , img/td tag. i'm able use pattern extracting them, used following patterns:
port -
$pattern = "/<img border\='0' src='images\/port\.png' width\='18' height\='18'>([a-za-z0-9\/ _-]{1,15})<\/td>/";
ip addr -
$pattern = "<img border\='0' src\='images\/ip\.png' width\='18' height\='18'>\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b <\/td>/";
and followed preg_match_all($pattern, $content, $matches);
. . .
but both of them return nothing me, tried following patterns:
port -
$pattern = "/<img border\='0' src='images\/port\.png' width\='18' height\='18'>(.*)<\/td>/";
ip addr -
$pattern = "<img border\='0' src\='images\/ip\.png' width\='18' height\='18'>(.*)<\/td>/";
...
but these pattern return like
<img border\='0' src='images\/port\.png' width\='18' height\='18'>fa0/0 <\/td>....(followed bunch of unwanted text , code) ......<\/td>
because (.*)
consider anyting between <img....>
, </td>
valid match
and also, tried specific ip address regex,$pattern = "/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/";
it return ip addresses me (like 111.22.3.119), unfortunately of link url in webpage contains ip address not want.
then tried $pattern = "/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}<\/td>\b/";
, returns nothing...
appreciate ppl willing me on this, thanks.
* edit 1 *
i tried $pattern = "/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b<\/td>/";
, works, don't know why, still figuring how solve port regex....
$pattern1 = '#<img[^>]+>([a-z][\w./-]{1,16})</td>#i'; $pattern2 = '#<img[^>]+>([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})</td>#';
Comments
Post a Comment