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
Share on other sites

you will have to echo the specifics of the array..try

print_r($div_array[0]);

 

Now the output is even more:

Array

(

    [0] => <div id="intro">

Hello I'm Something!

<p>asdoasduiasdasnda</p>

asdasdaksdjas<br/>

sdffdsg

</div>

)

 

I just wan't the text inside the <div> tags.

Link to comment
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>

Link to comment
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>

 

Now the output is:

Array

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

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.