1

Im trying to search for two HTML tags, and also grab everything in between. When I try each of the tags separately it finds them fine, so I know that they are spelled right and exist. The problem I think is with the pattern, can someone please help me with it. Everything I research online seems to go right over my head. So if you could please explain how your pattern works, that would be awesome!

See code below, and if you have any questions feel free to ask.

Thanks for your time:)

<?php
$date= date(Y)."/".date(n)."/".date(j);
$address= "http:www.example.com/".$date;
$text_page = file_get_contents("$address");

$searchfor1 = '<li id="menuSynchronizeSwitch">';
$searchfor2 = '<li  id="footerPrevWeek';

header('Content-Type: text/plain');

$pattern1 = preg_quote($searchfor1, '/');
$pattern2 = preg_quote($searchfor2, '/');

$pattern = "/^.*$pattern1.*\r*$pattern2.*\$/m";


if(preg_match_all($pattern, $text_page, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found";
}
?>
1

2 Answers 2

1

Try using $pattern = '/\<li id\="menuSynchronizeSwitch"\>.*\<li id\="footerPrevWeek/si';. Note the /s, which allows the dot to match newlines, which I suspect is what is causing this to fail.

1
  • Thanks that worked awesome! I just had to add a few backslashes and it worked. See code below. $pattern = '/\<li id\="menuSynchronizeSwitch"\>.*\<li id\="footerPrevWeek/si';
    – Dropoff510
    Oct 10, 2012 at 7:31
0

You'll need to group each pattern with parenthesis

like this:

$pattern = "/(some pattern)/";

and you can get each group by going through the $matches array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.