duanliu6083 2013-12-27 22:56
浏览 14
已采纳

需要帮助使用preg_match修复数据库中文章中的链接[关闭]

I have a database of articles that contains about 4,000 broken links in roughly 8,000 articles. About half of these broken links follow a pattern that, with the right PHP script, can be used to automatically fix those broken links.

The broken links in question contain links like this:

<a href="http://www.mydomain.tld/article1234-plus-a-slug-
that-varies-in-length.html">some text</a>

The "1234" part represents the unique ID of the article. It can be a three to five digit number between 101 and 10700. The database record for that unique ID contains the correct URL for the page.

I need to identify the strings in the HTML of each article that fit this pattern, extract the "1234" part of the URL, assign it to a $variable, and then replace the URL in the HTML markup of the article for that link with the correct URL taken from the database for that unique ID.

I suspect that preg_match is an appropriate function to use in this case, but I really can't see how to build the PHP script. (My PHP skills aren't great. Neither are my regex skills.) Can anyone help me with a simple script to handle this task?

  • 写回答

3条回答 默认 最新

  • dtjzpg5313 2013-12-27 23:21
    关注
    $url = '<a href="http://www.mydomain.tld/article1234-plus-a-slug-
    that-varies-in-length.html">some text</a>';
    
    preg_match("#article([0-9]+)#i", $url , $urlmatches);
    
    printf( 'id: %d' , (int) $urlmatches[1] );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?