How to get attribute from bbcode [URL] tag using regex

前端 未结 1 1484
南旧
南旧 2021-01-26 20:54

I have a string

$string = \'[URL=\"http://www.google.com\"]Google[/URL]

I am using the following code to convert it to html link



        
相关标签:
1条回答
  • 2021-01-26 21:39

    I think what you want is preg_match?

    Actually, you can just use the pattern you already have. It will give you the whole match and each match within round parantheses through a parameter.

    preg_match('/\[url=(.+?)\](.+?)\[\/url\]/i', $string, $matches);
    echo $matches[1]; // http://www.google.com
    echo $matches[2]; // Google
    echo $matches[0]; // [URL="http://www.google.com"]Google[/URL]
    

    The return value of preg_match is 1 (match found) or 0/false (no match/error).

    0 讨论(0)
提交回复
热议问题