Forum Moderators: coopster

Message Too Old, No Replies

Regex question

         

mooger35

6:03 pm on Jun 19, 2009 (gmt 0)

10+ Year Member



I've been looking over various examples on this site and on others trying to figure this out but I just can't get a result.

What I'm trying to do is pull information from a page (on my own server) and take various portions of it with preg_match_all (or at least that's what I think I should use).

On page I'm looking at (example.php)

<h1>Title</h1>
<ul>
<li><a href="page1.php">Page 1</a></li>
<li><a href="page2.php">Page 2</a></li>
<li><a href="page3.php">Page 3</a></li>
</ul>

What I want is to get a multidiminsional array as follows:

Array
(
[0] => Array
(
[0] => page1.php
[1] => page2.php
[2] => page3.php
)

[1] => Array
(
[0] => Page 1
[1] => Page 2
[2] => Page 3
)

)

Anyone help a frustrated guy out?

mooger35

6:35 pm on Jun 19, 2009 (gmt 0)

10+ Year Member



Not the most elegant way of doing it I'm sure but when your stuck... brute force works.

$data = file_get_contents('example.php');

$pattern = "/href=[\"']?([^\"']?.*(php))[\"']?.*(php)?/i";
preg_match_all($pattern, $data, $data_array);

$page_array = array();
foreach($data_array[0] as $key => $p){
$page_array['page'][$key] = end(explode('">',$p));
$page_array['link'][$key] = $data_array[1][$key];
}