Search

Laravel 5.5 String Helper Function: ascii

November 27, 2017 —John Koster

The ascii helper method accepts only one argument: $value. $value should always be a string, or something that can be cast into a string. The function converts a string in the UTF-8 encoding into its ASCII equivalent.

This method is useful when communicating with other software platforms that require ASCII, or when complying with protocols (such as HTTP) about how headers, or some values, need to be formatted.

#Signature

The signature of the ascii method is:

1public static function ascii(
2 $value,
3 $language = 'en' // Also accepts `bg` or `de`
4 );

#Example Use

The ascii method accepts a $language parameter (which defaults to en) that can be used:

1use Illuminate\Support\Str;
2 
3// Outputs "sht"
4echo Str::ascii('щ', 'bg');

The ascii method provides support for the following additional languages out of the box:

Language Language Parameter
Bulgarian bg
German de

In Laravel 5.5, the additional language features provided by the Laravel framework have been adapted from the danielstjules/stringy library.

G> Appendix D: Appendix D: Language Specific Replacements contains reference tables for each of the supported language-specific replacements.

1use Illuminate\Support\Str;
2 
3// The following will output "a test string"
4// in the ASCII encoding.
5 
6echo Str::ascii('a test string');

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.