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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题