API16

JString/compliant: Difference between revisions

From Joomla! Documentation

m clean up
m preparing for archive only
 
Line 5: Line 5:




{{subst:Description:JString/compliant}}
 


===Syntax===
===Syntax===
Line 45: Line 45:




{{subst:SeeAlso:JString/compliant}}
 


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=compliant
  category=compliant
  category=JString
  category=JString
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>

Latest revision as of 02:08, 25 March 2017

Description

Tests whether a string complies as UTF-8. This will be much faster than utf8_is_valid but will pass five and six octet UTF-8 sequences, which are not supported by Unicode and so cannot be displayed correctly in a browser. In other words it is not as strict as utf8_is_valid but it's faster. If you use is to validate user input, you place yourself at the risk that attackers will be able to inject 5 and 6 byte sequences (which may or may not be a significant risk, depending on what you are are doing) valid http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php#54805




Syntax

static compliant($str)
Parameter Name Default Value Description
$str UTF-8 string to check

Returns

boolean TRUE if string is valid UTF-8

Defined in

libraries/joomla/utilities/string.php

Importing

jimport( 'joomla.utilities.string' );

Source Body

public static function compliant($str)
{
        if (strlen($str) == 0) {
                return TRUE;
        }
        // If even just the first character can be matched, when the /u
        // modifier is used, then it's valid UTF-8. If the UTF-8 is somehow
        // invalid, nothing at all will match, even if the string contains
        // some valid sequences
        return (preg_match('/^.{1}/us',$str,$ar) == 1);
}



Examples

Code Examples