php - Preg_replace multiple patterns -


i'm busy website , element use preg_replace replace spaces dash.

preg_replace('/\\s/', '-', $item_replace_en[0]) 

and works great have new problem. user enterd following "music / film". output "music-/-film" , want "music-film".

how can accomplish this?

it looks you're trying generate string can used url.

there numerous of scenarios can happen when user adds title want convert url safe string. instance use this:

mess'd --text-- (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->");  

should return:

messd-up-text-just-to-stress-test-our-little-clean-url-function

is code ready that? in case can use function:

setlocale(lc_all, 'en_us.utf8'); function toascii($str, $replace=array(), $delimiter='-') {     if( !empty($replace) ) {         $str = str_replace((array)$replace, ' ', $str);     }      $clean = iconv('utf-8', 'ascii//translit', $str);     $clean = preg_replace("/[^a-za-z0-9\/_|+ -]/", '', $clean);     $clean = strtolower(trim($clean, '-'));     $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);      return $clean; } 

Comments

Popular posts from this blog

sql server - NHibernate incorrectly creating ManyToMany table - Cannot define PRIMARY KEY constraint on nullable column error -

All overlapping substrings matching a java regex -

c++ - Using OpenSSL in a multi-threaded application -