I am new to php. As a part of my course homework assignment , I am required to extract data from a website and using that data render a table.

P.S. : Using regex is not a good option but we are not allowed to use any library like DOM, jQuery etc.

Char set is UTF-8.

$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);

$patternform = '/<form(.*)<\/form>/sm';
preg_match_all($patternform ,$html,$matches);

Here regex works fine but when I apply the same regex for table tag, it return me empty array. Is there something to do with whitespaces in $html ?

What is wrong here?

有帮助吗?

解决方案

The following code produces a good result:

$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);

$patternform = '/(<table.*<\/table>)/sm';
preg_match_all($patternform ,$html,$matches);

echo $matches[0][0];

Result:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top