du7535 2015-08-29 11:36
浏览 11
已采纳

我如何使用正则表达式? (PHP)

I need to scrap data from a page, and the source code in the file is like this:

                <td class="pl-15">
                                            <a class="job-item" id="job-92837" href="http://www.jobs.com/job/looking-for-c-and-net-rockstar-developers/92837" >
                        Looking Rockstar Developers!                        </a>


                </td>
                <td >

                    <a href="http://www.jobs.com/employer/spidron/7388" class="joblist">    

                        Spidron                                             </a>

The Pattern i used is like this simply:

        $pattern = '/<a class="job-item" id="(.*?)" href="(.*?)">(.*?)\/a>/';

        preg_match_all($pattern, $content, $matches);

The problem with this pattern is that i get data in the thirds array like:

                Looking for Rockstar Developers!                        </a>


                </td>
                <td >

                    <a href="http://www.jobs.com/employer/spidron/7388" class="joblist">    

                        Spidron     

How do i get "Looking for Rockstar Developers!" in one array, the following link "http://www.jobs.com/employer/spidron/7388" in another array, and "Spidron" in another.

Just a beginner in using regular expression, help is much appreciated. :)

  • 写回答

1条回答 默认 最新

  • douyi1963 2015-08-29 11:53
    关注

    You have two problems here:

    1. Your data spans across multiple lines. Therefore you should add 's' at the end of your regex.
    2. You have a space before the closing tag. You should take it into consideration.

    Use this regex instead:

    $pattern = '/\<a class="job-item" id="(.*?)" href="(.*?)".*>(.*?)<\/a>/s';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么让ai定时给我发信息 c#或者python
  • ¥15 scrapy的Error
  • ¥20 ofstream覆盖写入
  • ¥15 RBF-VSG姚凤军论文复现问题
  • ¥30 开发一个APP商城在制作tabbar的时候运行不了代码没有检查出错误,但是显示不出tabbar,以下为运行结果,如何解决?
  • ¥15 多网卡服务器中winform如何绑定指定网卡
  • ¥15 看一看单片机上面的问题
  • ¥15 关于#python#pandas#的问题,想要实现:多个TXT导入Excel,进行分列,不同txt之间都从第一行开始,请各位专家解答!
  • ¥100 嘉南k510 开发板硬件接口
  • ¥15 cmake编译github上项目出错