Hack Forums
[PHP] Convert String to url friendly string - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Convert String to url friendly string (/showthread.php?tid=2855082)



Convert String to url friendly string - Webman.io - Sep 11, 2012

How can I somehow convert "a 16134 !#¤ toko5 14&!G90 into like a-16134-toko5-14-G90 or something like that, so that it can be used as a part of the url.


* - QuantumTunnel - Sep 11, 2012

Look it up on google preg functions in php :) Just use PHP's documentation you won't learn anything unless you go research it yourself :)


* - CarrotMilk - Sep 11, 2012

http://cubiq.org/the-perfect-php-clean-url-generator

If you google well enough, someone else has ALWAYS made a function


* - Dave85 - Sep 11, 2012

use this function:
PHP Code

<?php 
function to_url($text){

    
$text=trim($text);//remove space in first and end    
    
$text= strtr($text,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","aaooeecciiuuynn"); //remove accents
    
$text = preg_replace('/([^.a-z0-9]+)/i', '-', $text);   // remove  special character: other than letters and numbers change it to -
    
$text = strtolower($text); //lowercase
    
return $text;
} 



* - Cida - Sep 11, 2012

With the preg_replace() function.
PHP Code

<?php 
$string 
= preg_replace ('/[^a-zA-Z0-9]+/', '-', $string); # replace non-alphanumeric groups with a hyphen. 



* - houseinn2 - Sep 11, 2012

put your string in one variable.
make another variable valid char like "abc...xyz.ABC..XYZ012..9".
Check if the char in ur string is contained in valid char.
if yes,put it as it is
else,"-"


* - Webman.io - Sep 12, 2012

Thanks a lot to all of you. I got this solved.
:YEAH!: