我需要将此字符串转换为帖子url:
(带点和欧元符号的数字)例如:9000欧元->9000欧元
我找到了此函数,但不起作用:
add_filter( \'sanitize_title\', \'sanitize_title_extra\' );
function sanitize_title_extra( $title ) {
$friendlyURL = htmlentities($title, ENT_COMPAT, "UTF-8", false);
$friendlyURL = preg_replace(\'/&([a-z]{1,2}) (?:acute|lig|grave|ring|tilde|uml|cedil|caron);/i\',\'\\1\',$friendlyURL);
$friendlyURL = html_entity_decode($friendlyURL,ENT_COMPAT, "UTF-8");
$friendlyURL = preg_replace(\'/[^a-z0-9-]+/i\', \'-\', $friendlyURL);
$friendlyURL = preg_replace(\'/-+/\', \'-\', $friendlyURL);
$friendlyURL = str_replace( \'€\', \'euro\', $friendlyURL );
$friendlyURL = str_replace( \'€\', \'euro\', $friendlyURL );
$friendlyURL = str_replace( \'€\', \'euro\', $friendlyURL );
$friendlyURL = trim($friendlyURL, \'-\');
$friendlyURL = strtolower($friendlyURL);
return $friendlyURL;
}
谢谢!;)
SO网友:Stephen Harris
如果你看看source 你会发现这是因为sanitize_title
过滤器在remove_accents()
由调用sanitize_title()
.
但是,它会将原始标题作为第二个参数传递,以便您可以对原始标题执行任何替换,applyremove_accents()
并退回:
function wpsesanitize_title_extra( $title, $raw_title, $context ) {
$new_title = $raw_title;
$new_title = str_replace( \'€\', \'euro\', $new_title );
$new_title = str_replace( \'€\', \'euro\', $new_title );
$new_title = str_replace( \'€\', \'euro\', $new_title );
if( \'save\' == $context ){
$new_title = remove_accents( $new_title );
}
return $new_title;
}
add_filter( \'sanitize_title\', \'wpsesanitize_title_extra\', 5, 3 );