且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式:如何匹配任何字符串直到空格,或者直到标点符号后跟空格?

更新时间:2022-11-15 11:29:38

preg_replace('/
    \b       # Initial word boundary
    (        # Start capture
    (?:      # Non-capture group
    http|www # http or www (alternation)
    )        # end group
    .+?      # reluctant match for at least one character until...
    )        # End capture
    (        # Start capture
    [,.]+    # ...one or more of either a comma or period.
             # add more punctuation as needed
    )?       # End optional capture
    (\s|$) # Followed by either a space character or end of string
    /x', '<a href="\1">\1</a>\2\3'

...可能是你想要的.我认为它仍然不完美,但至少应该可以满足您的需求.

...is probably what you are going for. I think it's still imperfect, but it should at least work for your needs.

旁白:我认为这是因为 \b 也匹配标点符号

Aside: I think this is because \b matches punctuation too