Jump to content

Regular Expression returning empty Array ( )


Miteshsach86

Recommended Posts

Hi fellow developers,

 

I'm having a real problem at the moment, I'm trying to capture everything in between <body></body> tags using the following code but it does not print anything:

 

$lines = file("http://www.bbc.co.uk/");

 

foreach ($lines as $line_num => $line) {

$thecontent .= htmlspecialchars($line) . "<br />\n";

}

preg_match('/<body.*?>(.*?)<\/body >/', $thecontent, $htmltext);

$moretext = $htmltext[1];

echo $moretext;

 

When you do place a "print($thecontent);" into the code the entire html for [whatever the website] does display but I want to capture only the html code in between the body tags. I've tried everything but I just can't get this to work.  :shrug:

 

I would appreciate anyone's help and I'd like to thank you in advance.

 

M

Link to comment
Share on other sites

rather than having that foreach loop and individually adding each line to a variable, you can use file_get_contents() which will put the contents into a single string.

 

something like this may work... I'm not all too skilled with regular expressions though someone else may help you.

//haven't tested this yet, but good luck

$pageHtml = file_get_contents("http://www.bbc.co.uk/");

//get position of <body> tag
$openingTag = stripos($pageHtml, "<body>");

//position of </body> tag
$closingTag = stripos($pageHtml, "</body>");

//get length of body tag by position closing minus position starting tag (+6 for <body> tag characters)
$length = $closingTag - ($openingTag + 6);

$bodyHTML = substr($pageHTML, $length);

echo $bodyHTML;

Link to comment
Share on other sites

Hi Joel,

 

Thanks for your reply. I added thoses changes into my code but unfortunately that displays nothing at all. I've also posted another method which I tried and that displays an empty Array as well. I really need to use preg_match for what I'm doing. The new code is:

 

$thecontent = file_get_contents("http://www.bbc.co.uk/");

preg_match('/<body.*?>(.*?)<\/body >/', $thecontent, $htmltext);

$moretext = $htmltext[1];

echo $moretext;

 

I've searched everywhere for this problem but can't seem to figure it out. Please help  :confused:

 

M

Link to comment
Share on other sites

maybe try cURL?

demo here

 

 

and if you persist on using file() or file_get_contents(), this is written on PHP.net in regards to the file() function link

 

Tip

 

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the List of Supported Protocols/Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.