PHP URL for linking to Regex

I know that I saw this a lot, but I need something more than the norm. Unfortunately, when I look for it somewhere, it digs into messages about a simple html tag link connection. I want the PHP function to extract "http: //" and "https: //" from the link, as well as something after it. * So basically what I'm looking for is to turn A into B.

A: http://www.youtube.com/watch?v=spsnQWtsUFM B: <a href="http://www.youtube.com/watch?v=spsnQWtsUFM">www.youtube.com</a> 

If that helps, here is my current regular PHP replace function.

 ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\" class=\"bwl\" target=\"_new\">\\0</a>", htmlspecialchars($body, ENT_QUOTES))); 

Perhaps it would also be useful to say that I absolutely do not understand in regular expressions. Thanks!

EDIT: When I entered a comment like this blahblah https://www.facebook.com/?sk=ff&ap=1 blah , I get html like this <a class="bwl" href="blahblah https://www.facebook.com/?sk=ff&amp;ap=1 blah">www.facebook.com</a> , which doesn't work at all because it takes the text around the link with it. It works great if only someone comments on the link. This is when I changed the function to this

 preg_replace("#^(.*)//(.*)/(.*)$#",'<a class="bwl" href="\0">\2</a>', htmlspecialchars($body, ENT_QUOTES)); 
+6
source share
7 answers

This is a simple and clean way:

 $str = 'http://www.youtube.com/watch?v=spsnQWtsUFM'; preg_match("#//(.+?)/#", $str, $matches); $site_url = $matches[1]; 

EDIT: I assume $ str was checked as a url in the first place, so I left that out. In addition, I assume that all URLs will contain either "http: //" or "https: //". If the URL is formatted as follows: www.youtube.com/watch?v=spsnQWtsUFM or even youtube.com/watch?v=spsnQWtsUFM , the above regex will not work!

EDIT2: Sorry, I didnโ€™t understand that you were trying to replace all the lines as a whole. In this case, this should work the way you want:

 $str = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $str); 
+5
source

I also canโ€™t imagine the regex

 ^(.*)//(.*)/(.*)$ <a href="\1//\2/\3">\2</a> 

- this is what worked for me when I tried to use it to find and replace in a notebook.

^ (.) // should retrieve the protocol called \ 1 in the second line. (.) / should extract everything up to the first / - called \ 2 in the second line. (. *) $ captures everything to the end of the line. - is denoted as \ 3 in the second line.


Added later

 ^(.*)( )(.*)//(.*)/(.*)( )(.*)$ \1\2<a href="\3//\4/\5">\4</a> \7 

It should be a little better, but only 1 URL will be replaced

+2
source

\ 0 is replaced by the entire matching string, while \ x (where x is a number other than 0, starting from 1) will be replaced by each subpart of your matched string based on what you end in parentheses and the order of these groups appears . Your solution is as follows:

 ereg_replace("[[:alpha:]]+://([^<>[:space:]]+[:alnum:]*)[[:alnum:]/]", "<a href=\"\\0\" class=\"bwl\" target=\"_new\">\\1</a> 

I could not verify this, although so let me know if it works.

0
source

I think this should do it (I have not tested it):

 preg_match('/^http[s]?:\/\/(.+?)\/.*/i', $main_url, $matches); $final_url = '<a href="'.$main_url.'">'.$matches[1].'</a>'; 
0
source

I am surprised that no one remembers PHP parse_url :

 $url = 'http://www.youtube.com/watch?v=spsnQWtsUFM'; echo parse_url($url, PHP_URL_HOST); // displays "www.youtube.com" 

I think you know what to do next.

0
source

$result = preg_replace('%(http[s]?://)(\S+)%', '<a href="\1\2">\2</a>', $subject);

0
source

Regular expression code does not work fully.

I made this code. It is much more comprehensive, but it works:

See the result here: http://cht.dk/data/php-scripts/inc_functions_links.php

See the source code here: http://cht.dk/data/php-scripts/inc_functions_links.txt

0
source

Source: https://habr.com/ru/post/890803/


All Articles