Convert text links to html link

Asked

Viewed 1,748 times

3

I saw the solution from here Convert a text link to a href link and several others and eventually adapted to my need, but there are some problems.

I need to convert the links and the image links into image and hyperlink am doing the following.

$reply = preg_replace(
            "/(?<![\>https?:\/\/|href=\"'])(?<http>(https?:[\/][\/]|www\.)([a-z]|[A-Z]|[0-9]|[\/.;&?=#_\-\+\ ]|[~])*)/",
            "<a href=\"$1\">$1</a>",
            $reply
        );

        $reply = preg_replace('~<a[^>]*?href="(.*?(gif|jpeg|jpg|png|GIF|JPEG|JPG))".*?</a>~', '<img src="$1" />', $reply);

        $reply = preg_replace("/<a(.*?)>/", "<a$1 target=\"_blank\">", $reply);

example:

&lt;center&gt; &lt;a href=&quot;http://www.yoble.com.br/Community/434&quot;&gt; &lt;img src=&quot;https://lh4.ggpht.com/-05_v3FOUAlc/VHPHsM_MObI/AAAAAAAABeQ/4FodgebUMe0/s00/FDS043.png&quot;&gt;

the above code is to be viewed as code so that address should not be converted.

good note that I take any link and convert to a href, then I check if it is image and change the <a> for <img> and finally add to the links to target. The problems, links that contain "," do not work, when the user uses a link formatted with an image in the middle he tries to make this conversion, I would like to ignore these ready links and just add to target=\"_blank\">"

Thank you.

  • Your solution has some problems: (1) when the url is at the end of the line; (2) www.... generates invalid links; (3) generates double target=...;

  • You can give examples of strings you have at source and how you want them to appear later?

1 answer

3


To handle HTML editing, regular expressions are not the best Hithithite: is more suitable solutions based on html/xml parsers.

However, maintaining the approach of the original question:

$reply = preg_replace('~(?<![\'"])(https?://\S+(gif|jpe?g|png))~i',  ## jpeg  -> img
                      "<img src='$1'>", $reply);

$reply = preg_replace('~(?<![\'"])(https?://\S+)~',                  ## http  -> a href
                      "<a href='$1' target='_blank'>$1</a>", $reply);

$reply = preg_replace('~(?<![\'"/])(www.\w\S+)~',                    ## www.x -> a href
                      "<a href='http://$1' target='_blank'>$1</a>", $reply);

EDIT 1: (removed because it is contained in issue 2)

EDIT2: The replacement of URL for <f href='URL'>URL</a>, only occurs if URL does not is following ' or ".

To deal with previously noted link situations and other situations not to note, I added:

  1. a mark ''PrOtEcT'' before the address (as a slope ' to the address, this will not be noted)
  2. in the end we remove the mark

that is to say:

$contexto_esq = '   <a\s.*?>\s*           # proteger após <a...>
                  | &quot;                # proteger após &quot,
                ';

$txt= preg_replace("~($contexto_esq)( https? | www. )~ix",        # add ''PrOtEcT''
                      "$1''PrOtEcT''$2", $txt);

$txt= preg_replace('~(?<![\'"])(https?://\S+(gif|jpe?g|png))~i',  # jpeg -> img
                      "<img src='$1'>", $txt);

$txt= preg_replace('~(?<![\'"])(https?://\S+)~',                  # http -> a href
                      "<a href='$1' target='_blank'>$1</a>", $txt);

$txt= preg_replace('~(?<![\'"/])(www.\w\S+)~',                    # www. -> a hreg
                      "<a href='http://$1' target='_blank'>$1</a>", $txt);

$txt= preg_replace("~''PrOtEcT''~","",$txt);                      # remove ''PrOtEcT''

Test policy As the conditions of the exercise are not defined and are constantly changing, it is crucial to use a set of test examples (which ideally should be in the set)

$txt =  <<<EOD
1) anotar com <a>:
http://n.u.pt/  mais  www.di.br e http://www.di.br 
2) não anotar:
"http://n.u.pt/sss1"  e 'www.di.br' e 'http://www.di.br' 
3) anotar com <img>:
http://n.u.pt/sss2.jpg  e ainda http://www.di.br/dir/f.png
4) não anotar:
"http://n.u.pt/sss3.jpg"  e ainda 'http://www.di.br/dir/f.png'
5) já anotado:
<a href="http://n.u.pt/" target="_blank">http://n.u.pt/</a>
6) já anotadao
<a href="http://n.u.pt/" target="_blank">   http://n.u.pt/f.jpg</a>
7) não anotar:
href=&quot;http://www.yoble.com.br/Community/434&quot;&gt; &lt;img 

EOD;

In the present case it is producing:

1) anotar com <a>:
<a href='http://n.u.pt/' target='_blank'>http://n.u.pt/</a>  mais  <a href='http://www.di.br' target='_blank'>www.di.br</a> e <a href='http://www.di.br' target='_blank'>http://www.di.br</a>
2) não anotar:
"http://n.u.pt/sss1"  e 'www.di.br' e 'http://www.di.br'
3) anotar com <img>:
<img src='http://n.u.pt/sss2.jpg'>  e ainda <img src='http://www.di.br/dir/f.png'>
4) não anotar:
"http://n.u.pt/sss3.jpg"  e ainda 'http://www.di.br/dir/f.png'
5) já anotado:
<a href="http://n.u.pt/" target="_blank">http://n.u.pt/</a>
6) já anotadao
<a href="http://n.u.pt/" target="_blank">   http://n.u.pt/f.jpg</a>
7) não anotar:
href=&quot;http://www.yoble.com.br/Community/434&quot;&gt; &lt;img
  • This has a problem, as I use an editor the user can type something like <a href="http://i.imgur.com/5EpFaEd.jpg">http://i.imgur.com/5EpFaEd.jpg</a>

  • Daniel, I’ve put together a new version to fill the position you’re referring to. If you are finding more cases, it would help if you created a concrete example with the various cases that are not yet covered.

  • edit the post, see another example that doesn’t work.

  • See revision (Edit 2).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.