Monday 2 December 2019

php - Get content from html file



I have a list of html files. Each file repeatedly has the strings onClick="rpd(SOME_NUMBER)" . I know how to get the content from the html files, what I would want to do is get a list of the "SOME_NUMBER" . I saw that I might need to do a preg_match, but I'm horrible at regular expressions. I tried



$file_content = file_get_contents($url);    
$pattern= 'onClick="rpd(#);"';
preg_match($pattern, $file_content);



As you could imagine... it didn't work. What would be the best way to get this done? Thanks!


Answer



This should get it done:




$file_content ='234=fdf donClick="rpd(5);"as23 f2 onClick="rpd(7);" dff fonClick="rpd(8);"';
$pattern= '/onClick="rpd\((\d+)\);"/';

preg_match_all($pattern, $file_content,$matches);
var_dump( $matches);



The output is like this:





array (size=2)
0 =>
array (size=3)
0 => string 'onClick="rpd(5);"' (length=17)

1 => string 'onClick="rpd(7);"' (length=17)
2 => string 'onClick="rpd(8);"' (length=17)
1 =>
array (size=3)
0 => string '5' (length=1)
1 => string '7' (length=1)
2 => string '8' (length=1)


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...