Jump to content

[SOLVED] Problem with Preg_Match


Styles2304

Recommended Posts

If I shorten this to one line, it will populate an array but otherwise, it shows me an empty array. I thought at first it was because of white space but if I remove it all and put it on one long line, it still returns an empty array. Any ideas?

 

<?php
//Setting up the Patterns
$WeatherPattern = '/<td rowspan=\"3\" align=\"center\" class=\"weatherTempText\">(.*?)<\/td>
      <td align=\"center\" colspan=\"2\" class=\"weatherText\">
        
      <\/td>

    <\/tr>
    <tr>
      <td align=\"center\" class=\"weatherText\">WIND<\/td>
      <td align=\"center\" class=\"weatherText\">HUMIDITY<\/td>
    <\/tr>
    <tr>
      <td align=\"center\" class=\"weatherText\">(.*?) (.*?)<\/td>

      <td align=\"center\" class=\"weatherText\">(.*?)%<\/td>
    <\/tr>
    <\/table>
    <\/span>
  <\/td>
<\/tr>
<tr>
  <td class=\"radarimage\">
    <a href=\"\/content\/weather\"><img src=\"\/cache\/weather_current_conditions_radar_image\" border=0><\/a>

  <\/td>
<\/tr>
<tr>
  <td>
    <table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">
    <tr>

      <td class=\"weatherTable\" align=\"center\">
        <span class=\"weatherText\">(.*?)<\/span><br>
        <img src=\"(.*?)\" ><br>

        <span class=\"weatherTemp2Text\">(.*?)<\/span>
      <\/td>
      <td class=\"weatherTable\" align=\"center\">
        <span class=\"weatherText\">(.*?)<\/span><br>
        <img src=\"(.*?)\" ><br>
        <span class=\"weatherTemp2Text\">(.*?)<\/span>
      <\/td>

      <td class=\"weatherTable\" align=\"center\">
        <span class=\"weatherText\">(.*?)<\/span><br>
        <img src=\"(.*?)\" ><br>
        <span class=\"weatherTemp2Text\">(.*?)<\/span>/';

//Getting the Info
$Page = file_get_contents('http://mywabashvalley.com/index.php');
preg_match($WeatherPattern, $Page, $Output);
print_r($Output);
?>

Link to comment
Share on other sites

<pre>
<?php
$page = file_get_contents('http://mywabashvalley.com/index.php');
$result = array();
$patterns = array(
	'/(?<=weatherTempText">)(?P<current_temp>\d+°)/',
	'/(?<=weatherText">)(?P<current_wind>\d+\s+[A-Z]+)/',
	'/(?<=weatherText">)(?P<current_humidity>\d+%)/',
	'/(?<=weatherText">)(?P<day>[A-Z]{3})(?![A-Z]).*?<img src="(?P<img>[^"]+).*?Temp2Text">(?P<temp>\d+°)/s'
);
foreach ($patterns as $pattern) {
	preg_match_all($pattern, $page, $matches, PREG_SET_ORDER);
	$num_matches = count($matches);
	foreach ($matches as $match) {
		foreach (array_keys($match) as $key) {
			if (preg_match('/\A\d+\z/', $key)) {
				continue;
			}
			if ($num_matches == 1) {
				$result[$key] = $match[$key];
			}
			else {
				if (!array_key_exists($key, $result) || !is_array($result[$key])) {
					$result[$key] = array();
				}
				array_push($result[$key], $match[$key]);
			}

		}
	}
}
print_r($result);
?>
</pre>

 

Array

(

    [current_temp] => 66°

    [current_wind] => 13 SSE

    [current_humidity] => 88%

    [day] => Array

        (

            [0] => TUE

            [1] => WED

            [2] => THU

        )

 

    => Array

        (

            [0] => /media/gif/PrtlyCldyTstorm0030.gif

            [1] => /media/gif/PrtlyCldyTstorm0030.gif

            [2] => /media/gif/PrtlyCldy0031.gif

        )

 

    [temp] => Array

        (

            [0] => 84°

            [1] => 83°

            [2] => 90°

        )

 

)

 

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.