Laravel Helper Function: ASCII

November 16, 2016 —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 function 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.

Example use:

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');

As of Laravel 5, the ascii function internally uses the stringy library. Previous versions of Laravel have used the patchwork/utf8 package.

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.