使用preg_match用PHP提取内容

I am using preg_match() to extract this -

Mostly dry. Very mild (max 16°C on Fri afternoon, min 12°C on Tue night). Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night)

From this -

3 Day Weather Forecast Summary:</b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night). Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).</span>



My code isn't working and just returns Array ( )

$contents = "3 Day Weather Forecast Summary:<\/b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).</span>";

preg_match('/3 Day Weather Forecast Summary:<\/b><span class="read-more-small"><span class="read-more-content"> <span class=\"phrase\"> (.*?) </s', $contents, $matches);

print_r($matches);

I would say DOMDocument is your friend, but if you are really want to solve this with preg_match you cpuld try this one:

$contents = "3 Day Weather Forecast Summary:<\/b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).</span>";

preg_match( '@<span class="phrase">(.*?)</span>@s', $contents, $matches);

var_export( $matches );

UPDATE:

if you can't go for classes, try this:

preg_match( '@3 Day Weather Forecast Summary:.*?<span class="read-more-content"> <span class="phrase">(.*?)</span>@s', $contents, $matches);

The output will be:

Array
(
    [0] => <span class="phrase">Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).</span>
    [1] => Mostly dry. Very mild (max 16&deg;C on Fri afternoon, min 12&deg;C on Tue night).
Winds decreasing (fresh winds from the N on Wed morning, calm by Wed night).
)

For instance you want to match "Robert went." in "Robert Went.I never realized when.
" .You should use preg_match() in the following way:

$text = "<span style='color: #999;'>Robert Went.I never realized when.<br />";

$matches = array();

preg_match("/.*(Robert Went\.).*/", $text, $matches);

It seems that you are just removing all html code (the span tags) from the string (and the first part '3 Day Weather Forecast Summary:'). Why not detect all < and >? Something like:

$text = preg_replace('/<.*?>/', '', $text);
$text = trim(substr($text, strlen('3 Day Weather Forecast Summary:')));

The first line replaces all text within < and > (inclusive) by an empty string. The ? is there to make it not greedy so that only matching < and > are removed.

The second line simply removes the leading string. Since it may or may not have leading spaces, I also included the trim function, but that may not be necessary.

Obviously, these two lines may also be combined into one line.