1

Trying to convert a plain URL text into a valid link.

The problem I have is that my link might contain both English (A-Z/a-z) and Hebrew (אבגדהוזחטיכךלמםנןסעפףצץקרשת) letters.

Using PHP's urlencode() function I was able to get the correct format for Hebrew, yet I cannot find the right way in which I convert it into a link.

My code so far (does not work with Hebrew letters):

$replyText = preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $replyText);

An example for a URL I need to convert into a link:

google.co.il%2F%D7%A9%D7%9C%D7%95%D7%9D_Hello.html

Will become:

google.co.il%2F%D7%A9%D7%9C%D7%95%D7%9D_Hello.html

Silver007
  • 99
  • 9

2 Answers2

1

Despite what I believe you have posted to represent the desired output, if this was my task, I think I would have a urlencoded href value in the <a> tag and human-readable link text.

Code: (Demo)

$replyText = "google.co.il%2F%D7%A9%D7%9C%D7%95%D7%9D_Hello.html";
echo '<a href="', str_replace('%2F','/',$replyText), '">', urldecode($replyText), '</a>';

Source Code Output:

<a href="google.co.il/%D7%A9%D7%9C%D7%95%D7%9D_Hello.html">google.co.il/שלום_Hello.html</a>

Effective Output:

google.co.il/שלום_Hello.html

Notice that when you mouseover the link, your browser's status bar will show the un-encoded url anyhow.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

You just need to replace %2F => /, so your link will be: google.co.il/%D7%A9%D7%9C%D7%95%D7%9D_Hello.html

link

iwex
  • 384
  • 3
  • 17