Example 1: php strip out special characters
function clean($string) {
$string = str_replace(' ', '-', $string);
return preg_replace('/[^A-Za-z0-9\-]/', '', $string);
}
Example 2: php remove everything after character
$fullpath = 'folderName/file.ext';
$folder = substr($fullpath, 0, strpos($fullpath, '/'));
echo $folder;
Example 3: php remove after character
$s = 'Posted On April 6th By Some Dude';
echo strstr($s, 'By', true);
Example 4: remove invalid characters from a string laravel
function hyphenize($string) {
$dict = array(
"I'm" => "I am",
"thier" => "their",
);
return strtolower(
preg_replace(
array( '#[\\s-]+#', '#[^A-Za-z0-9. -]+#' ),
array( '-', '' ),
cleanString(
str_replace(
array_keys($dict),
array_values($dict),
urldecode($string)
)
)
)
);
}
function cleanString($text) {
$utf8 = array(
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
'/[ÍÌÎÏ]/u' => 'I',
'/[íìîï]/u' => 'i',
'/[éèêë]/u' => 'e',
'/[ÉÈÊË]/u' => 'E',
'/[óòôõºö]/u' => 'o',
'/[ÓÒÔÕÖ]/u' => 'O',
'/[úùûü]/u' => 'u',
'/[ÚÙÛÜ]/u' => 'U',
'/ç/' => 'c',
'/Ç/' => 'C',
'/ñ/' => 'n',
'/Ñ/' => 'N',
'/–/' => '-',
'/[’‘‹›‚]/u' => ' ',
'/[“”«»„]/u' => ' ',
'/ /' => ' ',
);
return preg_replace(array_keys($utf8), array_values($utf8), $text);
}