duankanyi6539 2016-08-04 21:34
浏览 35
已采纳

PHP Regex从url标记获取名称

There is a lot of Regex to get links or a value from url tags <a href , but what about extract a value from url tags like this

$text = '[URL="http://google.com"]ANY THING[/URL]';

if i want get value ANY THING from this url tag , what Regex Can I use ?

  • 写回答

1条回答 默认 最新

  • duanmajing9332 2016-08-04 21:41
    关注

    You can use this one

    '/\[URL[^]]+\](?P<name>[^\[]+)\[\/URL\]/'
    

    But you should probably learn Why.. Here is a good tester that shows that regx at work

    https://regex101.com/r/hS0sO5/1

    Traditionally this is called BBcode ( builtin board code )

    https://en.wikipedia.org/wiki/BBCode

    There are full PHP implementations of this sort of thing you can use besides regx

    If you want both ( optionally the url ) you can use this one

    '/\[URL(?:\=\"(?P<url>[^"]+)\")?\](?P<name>[^\[]+)\[\/URL\]/'
    

    And here is that one at work

    https://regex101.com/r/hS0sO5/2

    That last one does require the " around URL, I have seen them done with ' or no quotes at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改