Jump to content

Get div from webpage


Worqy

Recommended Posts

Hi.

 

I found this code for my website, and it work well. Its just that there comes some text I wan't to delete.

Code:

<?php
/*
index.php
*/
Session_start();
if($_SESSION['login'] == false)
{
header("Location:login.php");
}
$a = $_GET['a'];
$source='http://****.elementfx.com/test.php';

//$source='sample.txt';

$page_all = file_get_contents($source);

$div_array=array();

preg_match_all('#<div id="intro">(.*?)</div>#sim', $page_all, $div_array);

//print_r($div_array);




?>
<html>
<head>
<title>Home</title>
</head>
<body>
<center>
<p><b><font color="blue" size="20">*****</font></b> <font color="blue" size="2">version 0.9_01</font></p>
<br/>
<br/>
<br/>
<textarea cols="50" rows="10"><?php print_r($div_array[1]);?></textarea>
</center>
</body>
</html>	

 

The text it should get is:

Hello I'm Something!

<p>asdoasduiasdasnda</p>

asdasdaksdjas<br/>

sdffdsg

 

But the output is:

Array

(

    [0] =>

Hello I'm Something!

<p>asdoasduiasdasnda</p>

asdasdaksdjas<br/>

sdffdsg

 

)

I need to get rid of the Array( [0]... thing..

 

Regards

Worqy

Link to comment
https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/
Share on other sites

The function preg_match_all() is returning an array of matches.  print_r() will output the contents of an array. If all you want is the text within the div tags then just echo out the variable $div_array[1] rather than using print_r.

	<textarea cols="50" rows="10"><?php echo $div_array[1];?></textarea>

The function preg_match_all() is returning an array of matches.  print_r() will output the contents of an array. If all you want is the text within the div tags then just echo out the variable $div_array[1] rather than using print_r.

	<textarea cols="50" rows="10"><?php echo $div_array[1];?></textarea>

 

Now the output is:

Array

Use $div_array[1][0] instead maybe.

Worked, thanks!

 

I've tried all I know to this problem, but its been some months since I coded PHP, so I might have forgot something.

This line:

preg_match_all('#<div id="intro">(.*?)</div>#sim', $page_all, $div_array);

should look like this:

<div id=$a> etc..

But I cant get it to work!

Looks to me like your preg_match_all() is set to grab everything in between of the div tags. What us it returning?

 

It is returning everything I wan't it to..

But as you can see from the code in the first post, $a is a user input and I would wan't it to get from a div that the user selects

So $a will contain the id of a div you want to get, for example when $a is set to info only select the div with the id of info. In that case you can do this.

$a = 'info';
preg_match_all('#<div id="' . $a . '">(.*?)</div>#sim', $page_all, $div_array);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.