stripping an url from a text

2019-08-22 05:28发布

How do I get to strip a url and put it back in the same position?

标签: php regex url
5条回答
何必那么认真
2楼-- · 2019-08-22 05:54

Jeff Atwood had an informative post regarding the issues you might face with this.

Regardless of the programming language and libraries you use, you will have problems with some cases, e.g., --http://www.codinghorror.com/blog/archives/001181.html-- when using a straightforward regex substitution.

I don't use PHP very often, but the issue boils down to trying to figure out the pattern to use in the preg_replace call.

查看更多
做自己的国王
3楼-- · 2019-08-22 05:58

Use regular expressions.. you can easily detect the URL using regexs.

Detecting a URL

Replacing a Pattern

查看更多
霸刀☆藐视天下
4楼-- · 2019-08-22 06:09

If you're wanting to put the same URL back into its initial position, why do you want to remove it in the first place/ what exactly are you trying to do. A little more context would help us help you.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-08-22 06:17

you're probably looking for [preg_replace_callback](http://www.php.net. /preg_replace_callback).

it simply matches a regular expression pattern, gives the result to a function you provide, and replaces the original match with its return value.

查看更多
我想做一个坏孩纸
6楼-- · 2019-08-22 06:19

There are not many ways to regexp an url that is compliant with rfc3986

As a C string, the regex will be:

"^(([^:/?#]+):)?(//([^/?#]*)|///)?([^?#]*)(\\?[^#]*)?(#.*)?"
enum {
    URL = 0,
    SCHEME_CLN = 1,
    SCHEME  = 2,
    DSLASH_AUTH = 3,
    AUTHORITY = 4,
    PATH    = 5,
    QUERY   = 6,
    FRAGMENT = 7
};

Where the enum denotes what capture indexes correspond to which url parts.

查看更多
登录 后发表回答