php mb_ereg_replaceは、preg_replaceが意図したとおりに機能する間、置き換えられません
質問
私は、空の文字列ですべての非単語文字を文字列に交換しようとしています。スペースとすべての複数のスペースを1つのスペースとしてまとめることを期待しています。
次のコードがこれを行います。
$cleanedString = preg_replace('/[^\w]/', ' ', $name);
$cleanedString = preg_replace('/\s+/', ' ', $cleanedString);
しかし、MB_EREG_REPLACEを使用しようとしているとき、何も起こりません。
$cleanedString = mb_ereg_replace('/[^\w]/', ' ', $name);
$cleanedString = mb_ereg_replace('/\s+/', ' ', $cleanedString);
$ cleanedStringは、上記の場合に$ nameの場合と同じです。私は何が間違っているのですか?
解決
mb_ereg_replace
セパレーターは使用しません。以前にエンコードを指定する必要がある場合とそうでない場合もあります。
mb_regex_encoding("UTF-8");
//regex could also be \W
$cleanedString = mb_ereg_replace('[^\w]', ' ', $name);
$cleanedString = mb_ereg_replace('\s+', ' ', $cleanedString);
他のヒント
function create_slug_html($string, $ext='.html'){
$replace = '-';
$string=strtolower($string);
$string=trim($string);
mb_regex_encoding("UTF-8");
//regex could also be \W
$string= mb_ereg_replace('[^\w]', ' ', $string);
$string= mb_ereg_replace('\s+', ' ', $string);
//remove query string
if(preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$string)){
$parsed_url = parse_url($string);
$string = $parsed_url['host'].' '.$parsed_url['path'];
//if want to add scheme eg. http, https than uncomment next line
//$string = $parsed_url['scheme'].' '.$string;
}
//replace / and . with white space
$string = preg_replace("/[\/\.]/", " ", $string);
// $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//remove multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//convert whitespaces and underscore to $replace
$string = preg_replace("/[\s_]/", $replace, $string);
//limit the slug size
$string = substr($string, 0, 200);
//slug is generated
return ($ext) ? $string.$ext : $string;
}
確認してください、それは大丈夫で、英語とユニコードをサポートしてください
入力はそうではありません マルチバイト 従って mb
機能が失敗します。
所属していません StackOverflow