Regex PHP: get specific content from another website

时间:2015-06-26 09:56:03

标签: php regex preg-match

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?

2 个答案:

答案 0 :(得分:3)

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);

答案 1 :(得分:1)

并逃脱元字符 $ date ='#\\开催时间\&lt; / th&gt; \(。+?)\&lt; / td&gt;&lt; / tr&gt; #s'; preg_match($ date,$ page,$ dates);