0

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?

9
  • Why are you not allowed... homework? Commented Mar 8, 2013 at 18:59
  • 2
    You should read this How to parse and process HTML/XML with PHP Any class telling you to use regex over DOM is a class you should un-enroll from and get a refund. Commented Mar 8, 2013 at 18:59
  • What information do you need? Target only the specific fields you need, build an array of objects, and then display them in a table. Where are you stuck exactly? Commented Mar 8, 2013 at 19:01
  • We are not allowed to use any external library because they want us to learn the hard way, loose our sleep, get cranky and then post questions on forums for HELP !! Commented Mar 8, 2013 at 19:02
  • @Margi PHP DOM is not an external library it is part of PHP, check out the above link. Commented Mar 8, 2013 at 19:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.