Regex PHP:从其他网站获取特定内容

Regex PHP:从其他网站获取特定内容

问题描述:

I'm writing a PHP script that will fetch specific codes from the other site.

All is done and its fetching a div which has a class and showing it on my page.

$title = '#\<div class="detailBox"\>(.+?)\<\/div\>#s';
preg_match($title, $page, $titles);
echo $titles[0];

But when I try to fetch from a table it shows nothing

The content on the other site is like this:

<tr>
   <th scope="row">開催時間</th>
   <td>10:30~17:00</td>
</tr>

And I'm using this code to fetch it:

$date = '#\<tr\><th scope="row"\>開催時間<\/th\><td\>(.+?)\<\/td\><\/tr\>#s';
preg_match($date, $page, $dates);
echo $dates[0];

I have tried $date = '#\<th scope="row"\>開催時間<\/th\>#s'; and it works but not if I add <td> in it.

What I'm doing wrong?

You need to turn on the unicode modifier u and also match the inbetween line breaks.

$date = '#<tr>\s*<th scope="row">開催時間</th>\s*<td>(.+?)</td>\s*</tr>#su';
preg_match($date, $page, $dates);

and escape the metacharacters $date = '#\\開催時間\</th>\(.+?)\</td></tr>#s'; preg_match($date, $page, $dates);