tags:

views:

256

answers:

5

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

+6  A: 

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

Detecting a URL

Replacing a Pattern

Chathuranga Chandrasekara
I read that, it just says detecting, my question is extracting and replacing it back in the same position.
Check the second link
Chathuranga Chandrasekara
A: 

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.

Evan Meagher
supposing the url is http://yahoo.com I need to reformat it as <a href="http://www.yahoo.com">Yahoo</a>
+1  A: 

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.

piotr
A: 

you're probably looking for 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.

Schnalle
A: 

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

http://www.codinghorror.com/blog/archives/001181.html

Regardless of the programming language and libraries you use, you will have problems if, say, with some cases --http://www.codinghorror.com/blog/archives/001181.html-- with 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.

Sinan Ünür
The link to Jeff's article is purposely adjacent to -- to highlight the difficulty of determining where a URL ends when link detection is done automatically. To access the article, remove the two dashes from the end of the URL.
Sinan Ünür