<?php $string = "[ABC] This & Text"; Echo make_post_type($string); function make_post_type($string) { $needle = array('-', ' '); $arr = explode(" ", $string); foreach($arr as &$a){ if($a[0] != "[" && $a[-1] != "]"){ $a = preg_replace("/[^a-zA-Z0-9_\s]/", "", strtolower($a)); // Remove special characters }else{ $a = substr($a, 1,-1); } } $string = preg_replace('!\s+!', ' ', implode(" ", $arr)); // Now remove extra spaces return str_replace($needle, '_', $string); }
You have javascript disabled. You will not be able to edit any code.