You can use mb_strlen() to get the byte length using a encoding that only have byte-characters, without worring about multibyte or singlebyte strings. For example, as drake127 saids in a comment of mb_strlen, you can use '8bit' encoding:

<?php
    $string = 'Cién cañones por banda';
    echo mb_strlen($string, '8bit');
?>

You can have problems using strlen function since php have an option to overload strlen to actually call mb_strlen. See more info about it in http://php.net/manual/en/mbstring.overload.php

For trim the string by byte length without split in middle of a multibyte character you can use:

mb_strcut(string $str, int $start [, int $length [, string $encoding ]] )

Update for PHP 8.0 or great: Since PHP 8.0, Function Overloading was removed so you can always use strlen() to check the lenght in bytes.

Answer from PhoneixS on Stack Overflow
🌐
PHP
php.net › manual › en › function.strlen.php
PHP: strlen - Manual
As the manual says: "strlen() returns the number of bytes rather than the number of characters in a string.", so if you want to get the number of characters in a string of UTF8 so use mb_strlen() instead of strlen().
Discussions

What is the maximum length of a String in PHP? - Stack Overflow
I figure there has to be some kind of limit. What happens when a string gets too large? Is it concatenated, or does PHP throw an exception? ... @sAc I want to know more about PHP. I know i can't use php for memory heavy operations due to serious memory leaks. ... I would change the title, because not OP and not answers are answering the main question about length, they are all talking about size... More on stackoverflow.com
🌐 stackoverflow.com
The state of PHP unicode in 2019
PHP doesn't really manage unicode at all. They tried, and that was one of the factors that led to PHP 6 never becoming a thing. So instead, they decided to not have unicode strings at all - you only get byte arrays (which you may write as string literals). If you want actual strings, you have to implement most of it yourself, PHP only gives you a couple of primitives that you can use to operate on various string encodings (including utf-8 and other Unicode encodings) at the byte array level. So basically much the same deal as in C, except that PHP is supposed to be a high-level programming language that takes care of these things for you. More on reddit.com
🌐 r/lolphp
60
27
June 24, 2019
How to remove numbers from end of the string
howdy jishnutp, this seems to do the job ... PM(.*)\d{1,}$ it finds PM and any final digits, then grabs anything between them. it works with your data set on regex101.com if you enable "multiline". take care, lee More on reddit.com
🌐 r/regex
7
3
June 26, 2017
increment number at the end of a string each time you find it with str_replace?
= count($arr)-1) break; $out .= '{image:'.$idx.'}'; } echo $out; Demo The idea is to explode the string on {image}, thus removing it, and then to glue it back together using {image:n} as glue with n increasing with each iteration. More on reddit.com
🌐 r/PHPhelp
8
3
September 26, 2020
🌐
Medium
alex-daubois.medium.com › the-hell-of-calculating-the-size-of-strings-in-php-9d82b7fb4b8a
The Hell of Calculating The Size of Strings in PHP | by Alexandre Daubois | Medium
December 11, 2022 - As you can imagine, 255 is not enough space for all characters we know : Chinese, Japanese, French, Hindi, Arab alphabets and all other existing ones represent thousands, dozens of thousands of different symbols. We had to find a solution, which is called UTF. To simplify, it has been decided that characters could be encoded with more… ... CTO @ Les-Tilleuls.coop / PHP, FrankenPHP & Symfony Core Team / Space and Quantum Computing Enthusiast
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-find-string-length-in-php.php
How to Find String Length in PHP
PHP Array Functions PHP String Functions PHP File System Functions PHP Date/Time Functions PHP Calendar Functions PHP MySQLi Functions PHP Filters PHP Error Levels ... You can simply use the PHP strlen() function to get the length of a string.
🌐
GitHub
yellowduck.be › posts › counting-string-length-and-byte-size-using-php
Counting string length and byte size using PHP - YellowDuck.be
August 23, 2023 - You can use the strlen function to check the string size in bytes. The strlen function returns the number of bytes and not the number of characters.
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php strlen
PHP strlen(): Get the Length of a String in Bytes Examples
April 7, 2025 - The following example uses the strlen() function to return the length of the string PHP: <?php $str = 'PHP'; echo strlen($str); // 3Code language: PHP (php) ... The following multibyte string has five characters. However, its size is 15 bytes.
🌐
ReqBin
reqbin.com › code › php › 8ob9jfdb › php-string-length-example
How can I get the length of a string in PHP?
You can use the mb_strlen() function to get the number of visible characters. The following is the syntax of the mb_strlen() function: ... utf8 (optional): the parameter is a character encoding.
Find elsewhere
🌐
Udemy
blog.udemy.com › home › it & development › web development › the php strlen function: getting the php string length
The PHP STRLEN Function: Getting the PHP String Length - Udemy Blog
April 14, 2026 - And since the PHP STRLEN function has a function return and returns the number, you don’t need to print the STRLEN. You could also save the number of characters to a variable, like so: <?PHP $hello = "Hello, world!"; $string_length = strlen($hello); ?>
🌐
3D Bay
clouddevs.com › home › php guides › php’s strlen() function: measuring string lengths
PHP's strlen() Function: Measuring String Lengths
December 27, 2023 - The length of this string is 13 characters, including spaces and punctuation. When working with multibyte character encodings like UTF-8, strlen() may not give you the expected results because it counts bytes, not characters.
🌐
W3Schools
w3schools.com › php › func_string_strlen.asp
PHP strlen() Function
PHP Strings String Functions Modify Strings Concatenate Strings Slicing Strings Escape Characters PHP Numbers PHP Casting PHP Math PHP Constants PHP Magic Constants PHP Operators PHP If...Else...Elseif
🌐
TutorialKart
tutorialkart.com › php › php-string-length
How to get Length of String in PHP?
May 7, 2023 - Pass the string as argument to strlen() and the function returns an integer representing the length of string. In the following example, we will take a PHP String literal and find its length using strlen() function.
🌐
Reintech
reintech.io › blog › phps-strlen-function-practical-guide-measuring-string-length
PHP's `strlen()` Function: A Practical Guide for Measuring String Length
The strlen() function is a fundamental PHP string manipulation tool that returns the number of bytes in a string.
🌐
Learnsic
learnsic.com › blog › the-php-strlen-function-getting-the-php-string-length
The PHP STRLEN Function: Getting the PHP String Length
This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy · Got it
Top answer
1 of 6
110

http://php.net/manual/en/language.types.string.php says:

Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)

In PHP 5.x, strings were limited to 231-1 bytes, because internal code recorded the length in a signed 32-bit integer.


You can slurp in the contents of an entire file, for instance using file_get_contents()

However, a PHP script has a limit on the total memory it can allocate for all variables in a given script execution, so this effectively places a limit on the length of a single string variable too.

This limit is the memory_limit directive in the php.ini configuration file. The memory limit defaults to 128MB in PHP 5.2, and 8MB in earlier releases.

If you don't specify a memory limit in your php.ini file, it uses the default, which is compiled into the PHP binary. In theory you can modify the source and rebuild PHP to change this default value.

If you specify -1 as the memory limit in your php.ini file, it stop checking and permits your script to use as much memory as the operating system will allocate. This is still a practical limit, but depends on system resources and architecture.


Re comment from @c2:

Here's a test:

<?php

// limit memory usage to 1MB 
ini_set('memory_limit', 1024*1024);

// initially, PHP seems to allocate 768KB for basic operation
printf("memory: %d\n",  memory_get_usage(true));

$str = str_repeat('a',  255*1024);
echo "Allocated string of 255KB\n";

// now we have allocated all of the 1MB of memory allowed
printf("memory: %d\n",  memory_get_usage(true));

// going over the limit causes a fatal error, so no output follows
$str = str_repeat('a',  256*1024);
echo "Allocated string of 256KB\n";
printf("memory: %d\n",  memory_get_usage(true));
2 of 6
17

String can be as large as 2GB.
Source

🌐
HashBangCode
hashbangcode.com › article › work-out-size-bytes-php-string
Work Out Size In Bytes Of A PHP String | #! code
I found this very handy function on the php.net site in the user comments for the strlen() function. It accepts a string in ASCII or UTF-8 format and finds out how long that string is in bytes. The function works by going through the string and adding how many bytes each character represents.
🌐
OnlinePHP
onlinephp.io › strlen
strlen - Online Tool
PHP Functions · String Manipulation · strlen · Execute strlen with this online tool strlen() - Get string length · Strlen Online Tool · Manual · Code Examples · Strlen Online Tool · Manual · Code Examples · addcslashes · addslashes ...
🌐
Altorouter
altorouter.com › home › php string mastery: unleash power with length manipulation
String Length PHP: A Comprehensive Guide - Altorouter
January 22, 2024 - Mastering string length functions is crucial whether you’re validating user input, truncating text, or performing complex string operations. PHP’s simplest method of determining how long a string is is strlen(). This function returns the number of characters in a given string.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-get-string-length-in-php
How to get String Length in PHP ? - GeeksforGeeks
July 23, 2025 - Approach: This task can be done by using the built-in function strlen() in PHP. This method is used to return the length of the string.
🌐
Coderwall
coderwall.com › p › qgeuna › php-string-length-the-right-way
[PHP] String Length, The Right Way (Example)
August 26, 2019 - Note : There is no simple answer about the 1~6 bytes letters, so we wont go deep into it now, maybe another tutorial. well if you're sure the string wont has any unicode letters or you really want to count the bytes not letters then strlen() ...