Want to use urlencode in this function - how ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Reimer
Forum Newbie
Posts: 1
Joined: Wed Oct 09, 2002 6:59 pm

Want to use urlencode in this function - how ?

Post by Reimer »

Code: Select all

function make_clickable($text) 
{ 

   // pad it with a space so we can match things at the start of the 1st line. 
   $ret = " " . $text; 

   // matches an "xxxx://yyyy" URL at the start of a line, or after a space. 
   // xxxx can only be alpha characters. 
   // yyyy is anything up to the first space, newline, or comma. 
        
   $ret = preg_replace("#(&#1111;\n ])(&#1111;a-z]+?)://(&#1111;a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#i", "\\1<a href="\\2://\\3" target="_blank">\\2://\\3</a>", $ret); 
    
   // matches a "www.xxxx.yyyy&#1111;/zzzz]" kinda lazy URL thing 
   // Must contain at least 2 dots. xxxx contains either alphanum, or "-" 
   // yyyy contains either alphanum, "-", or "." 
   // zzzz is optional.. will contain everything up to the first space, newline, or comma. 
   // This is slightly restrictive - it's not going to match stuff like "forums.foo.com" 
   // This is to keep it from getting annoying and matching stuff that's not meant to be a link. 
   $ret = preg_replace("#(&#1111;\n ])www\.(&#1111;a-z0-9\-]+)\.(&#1111;a-z0-9\-.\~]+)((?:/&#1111;a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]*)?)#i", "\\1<a href="http://www.\\2.\\3\\4" target="_blank">www.\\2.\\3\\4</a>", $ret); 
    
   // matches an email@domain type address at the start of a line, or after a space. 
   // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".". 
   $ret = preg_replace("#(&#1111;\n ])(&#1111;a-z0-9\-_.]+?)@(&#1111;\w\-]+\.(&#1111;\w\-\.]+\.)?&#1111;\w]+)#i", "\\1<a href="mailto:\\2@\\3">\\2@\\3</a>", $ret); 

   // Remove our padding.. 
   $ret = substr($ret, 1); 

   return($ret); 
&#125;
This function replaces all http://... to <a href="http://... in a text. I would like to use the function urlencode to convert the URL, but until now I was not able to do this :(

Thanks
Reimer
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

in a url like http://the.server/the/path/page?param1=1&param2=true you only need to check the values 1 and true
I would take the string beginning after ? (if there) and then extract the key=value-pairs. i.e.

Code: Select all

&lt;?php
$pattern = '#\&amp;?(&#1111;^=]+)(&#1111;^\&amp;]*)#';
$data = 'param1=1&amp;param2=true';
preg_match_all($pattern, $data, $matches);

echo '&lt;pre&gt;';
print_r($matches);
echo '&lt;/pre&gt;';
?&gt;
Post Reply