La

30

Tam

2010

9:41

Changing URL to link with PHP

Writing and publishing text is one of the main activities people do while browsing content on the net. While contributing a website visitors may include links in their submission. When the content is displayed links in the text do not automatically turn into clickable links thus decreasing the usability of the site. Programmers can easily convert dummy URL addresses into links in PHP scrips with a simple preg_replace() function call. The following code snippet shows an example.

$s = 'http://mattimattila.fi';
$s = preg_replace('/([w]+://[w-?&;#~=./@]+[w/])/i',
  '<a href="$1">$1</a>', $s);

With complex regular expressions you can convert virtually anything in a string. Handy preg_replace() function is very powerful tool and much faster than deprecated old ereg_replace() function. As of PHP 5.3 and 6.0 this outdated function will no longer be supported.

Kirjoita kommentti

Kerro mielipiteesi ja kirjoita kommentti tai viittaa kirjoitukseeni omassa päiväkirjassasi käyttämällä trackback-osoitetta. Kirjoita kommenttisi ilman muotoilua. Anna sähköpostiosoitteesi, jos haluat, että siihen rekisteröity Gravatar näkyy kommentin yhteydessä. Sähköpostiosoitettasi ei julkaista.

Spammers please note, that your comment is mechanically reviewed with Akismet and automatically rejected when found as spam, so there is no use to fill out this comment form if you intend to send a spam comment, sorry.