サポート » バグ報告と提案 » 投稿編集時における日本語タイトルのパーマリンク部分について

  • 解決済 tmatsuur

    (@tmatsuur)


    投稿ページのパーマリンクについてですが、投稿タイトルが長い場合に
    「投稿タイトルの前方」. '…' .「投稿タイトルの後方」のように
    整形されて表示されているかと思います。
    投稿タイトルにマルチバイト(日本語など)の含まれる場合、マルチバイトの
    途中で切られてしまい、文字化けになることがあります。

    該当するソースコード(wp-admin/includes/post.phpのget_sample_permalink_html関数)は、

    if ( function_exists( 'mb_strlen' ) && mb_strlen( $post_name ) > 30 ) {
    	$post_name_abridged = mb_substr( $post_name, 0, 14 ) . '…' . mb_substr( $post_name, -14 );
    } elseif ( strlen( $post_name ) > 30 ) {
    	$post_name_abridged = substr( $post_name, 0, 14 ) . '…' . substr( $post_name, -14 );
    } else {
    	$post_name_abridged = $post_name;
    }

    となっており、マルチバイトも30文字で判定しているため、マルチバイト文字で
    30文字以下でシングルバイトで30文字を超えるケースでは、シングルバイトの
    調整が行われてしまい、これが文字化けの原因となってしまいます。

    現状WordPressの内部エンコーディングはUTF-8なので、ほとんどの日本語は1文字
    3バイトになっているため、マルチバイト時の調整を次のようにするのが妥当では
    ないかと考えます。

    if ( function_exists( 'mb_strlen' ) && mb_strlen( $post_name ) > 10 ) {
    	$post_name_abridged = mb_substr( $post_name, 0, 5 ) . '…' . mb_substr( $post_name, -5 );
    } elseif ( strlen( $post_name ) > 30 ) {
    	$post_name_abridged = substr( $post_name, 0, 14 ) . '…' . substr( $post_name, -14 );
    } else {
    	$post_name_abridged = $post_name;
    }

    これであればマルチバイト10文字まではそのまま表示され、マルチバイト11文字以上は
    マルチバイトの調整が、シングルバイト31文字以上はシングルバイト以上の調整が
    行われるようになり、文字化けが発生しにくくなると思います。

    厳密には、UTF-8の2バイト文字やiOSの絵文字のような4バイト文字を考慮しないと
    いけないのかもしれませんが。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 tmatsuur

    (@tmatsuur)

    よくよく考えると、次の方が無難かもしれません。

    if ( strlen( $post_name ) > 30 ) {
    	if ( function_exists( 'mb_strlen' ) && mb_strlen( $post_name ) != strlen( $post_name ) ) {
    		$post_name_abridged = mb_substr( $post_name, 0, 5 ) . '…' . mb_substr( $post_name, -5 );
    	else
    		$post_name_abridged = substr( $post_name, 0, 14 ) . '…' . substr( $post_name, -14 );
    } else {
    	$post_name_abridged = $post_name;
    }

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「投稿編集時における日本語タイトルのパーマリンク部分について」には新たに返信することはできません。