Worqy Posted May 5, 2011 Share Posted May 5, 2011 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 More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 you will have to echo the specifics of the array..try print_r($div_array[0]); Link to comment https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1210892 Share on other sites More sharing options...
Worqy Posted May 5, 2011 Author Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1210956 Share on other sites More sharing options...
wildteen88 Posted May 5, 2011 Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1210961 Share on other sites More sharing options...
Worqy Posted May 5, 2011 Author Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1210974 Share on other sites More sharing options...
wildteen88 Posted May 5, 2011 Share Posted May 5, 2011 Use $div_array[1][0] instead maybe. Link to comment https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211010 Share on other sites More sharing options...
Worqy Posted May 6, 2011 Author Share Posted May 6, 2011 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 https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211371 Share on other sites More sharing options...
fugix Posted May 6, 2011 Share Posted May 6, 2011 Looks to me like your preg_match_all() is set to grab everything in between of the div tags. What us it returning? Link to comment https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211378 Share on other sites More sharing options...
Worqy Posted May 6, 2011 Author Share Posted May 6, 2011 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 https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211387 Share on other sites More sharing options...
Worqy Posted May 6, 2011 Author Share Posted May 6, 2011 Bump. Anyone? Link to comment https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211542 Share on other sites More sharing options...
wildteen88 Posted May 6, 2011 Share Posted May 6, 2011 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 https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211547 Share on other sites More sharing options...
Worqy Posted May 6, 2011 Author Share Posted May 6, 2011 Worked! Thanks! Link to comment https://forums.phpfreaks.com/topic/235599-get-div-from-webpage/#findComment-1211654 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.