Jump to content

preg_match_all question??


TFD3

Recommended Posts

In this script:

http://www.alabamaweather.org/scripts/test2.txt

 

I have it set where it looks in the URL and find the <title></title> tags and displays all the data that is in between them.

However, the URL that is being read in the script had more than one <title> tags. How can I get the script to display ALL of the info that is in between the title tags?

 

Thanks for your help!

Link to comment
https://forums.phpfreaks.com/topic/74230-preg_match_all-question/
Share on other sites

From that code above I get this:

 

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

    [1] => Array
        (
            [0] => Storm Prediction Center Forecast Products
            [1] => NOAA's National Weather Service
            [2] => SPC - No watches are valid as of Fri Oct 26 21:07:02 UTC 2007
            [3] => SPC MD 2138
        )

)

 

I would like to get just this for example if at all possible:

 

Storm Prediction Center Forecast Products
NOAA's National Weather Service
SPC - No watches are valid as of Fri Oct 26 21:07:02 UTC 2007
SPC MD 2138

 

Anyone know what to change/add to the script?

Im getting this:

 

Array
(
    [0] => Storm Prediction Center Forecast Products
    [1] => NOAA's National Weather Service
    [2] => SPC - No watches are valid as of Fri Oct 26 21:29:01 UTC 2007
    [3] => SPC MD 2138
)

 

You have to go through the array... Try to take some time and read through the PHP manual

 

<pre><?php

$data = file_get_contents('http://www.spc.noaa.gov/products/spcrss.xml');

preg_match_all('|<title>(.*)</title>|U', $data, $headers);

$count = count($headers[1]);

for ($x = 0; $x <= $count-1; $x++) {
  echo $headers[1][$x]."\n";
}

echo "\n\n  OR  \n\n";

foreach ($headers[1] as $header){
  echo $header."\n";
}

echo "\n\n  OR  \n\n";

$x = 0;
while ($x <= $count-1) {
  echo $headers[1][$x]."\n";
  $x++;
}

echo "\n\n  OR  \n\n";

echo implode("\n", $headers[1]);

?>
</pre>

 

There are plenty different ways to accomplish this..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.