Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_check_invalid_utf8 [ WordPress Function ]

wp_check_invalid_utf8 ( $string, $strip = false )
Parameters:
  • (string) $string The text which is to be checked.
  • (boolean) $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
Returns:
Defined at:



Checks for invalid UTF8 in a string.

Source


<?php
function wp_check_invalid_utf8$string$strip false ) {
    
$string = (string) $string;

    if ( 
=== strlen$string ) ) {
        return 
'';
    }

    
// Store the site charset as a static to avoid multiple calls to get_option()
    
static $is_utf8;
    if ( !isset( 
$is_utf8 ) ) {
        
$is_utf8 in_arrayget_option'blog_charset' ), array( 'utf8''utf-8''UTF8''UTF-8' ) );
    }
    if ( !
$is_utf8 ) {
        return 
$string;
    }

    
// Check for support for utf8 in the installed PCRE library once and store the result in a static
    
static $utf8_pcre;
    if ( !isset( 
$utf8_pcre ) ) {
        
$utf8_pcre = @preg_match'/^./u''a' );
    }
    
// We can't demand utf8 in the PCRE installation, so just return the string in those cases
    
if ( !$utf8_pcre ) {
        return 
$string;
    }

    
// preg_match fails when it encounters invalid UTF8 in $string
    
if ( === @preg_match'/^./us'$string ) ) {
        return 
$string;
    }

    
// Attempt to strip the bad chars if requested (not recommended)
    
if ( $strip && function_exists'iconv' ) ) {
        return 
iconv'utf-8''utf-8'$string );
    }

    return 
'';
}
?>

Examples [ wp-snippets.com ]

Google Arama Sonuçlarý

Dahasý ...

0 User Note(s)

Henüz yok. Ýlk sen ol!

Yeni Ekle ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics