What is the maximum length of a String in PHP?
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));
Limit String Length
You can use something similar to the below:
if (strlen($str) > 10)
$str = substr($str, 0, 7) . '...';
What is the limit in size of a Php variable when storing string?
So, is there a limit in size to a Php variable to stores value, in this case, a string ?
Yes. A string can be as large as up to 2GB (2147483647 bytes maximum). You can override this limit by increasing memory_limit directive in php.ini.
From php7 there's not that limitation in a 64 bit system:
Support for strings with length >= 2^31 bytes in 64 bit builds.
How to limit string to some character in php
the_content()
will output the content into the page. It does not return
, it echo
s. I believe to get the content in a variable you need to use get_the_content()
.
This is only my passing knowledge of Wordpress though, I'd be really surprised if there wasn't such an excerpt function already built in.
What is the maximum length of a regular expression?
Edit: As @Jonny 5 points out, my test was flawed. However, the correct answer is 32767, or if you see the second bit of my answer, 64k.
I've just tested it on my local machine using the following:
$str = str_repeat('a', 256*1024);
$subject = "";
$pattern = '/^' . $str . '/';
preg_match($pattern, $subject, $matches);
and I got:
Warning: preg_match(): Compilation failed: regular expression is too large at offset 262145
In fact, you could have larger if you desired. Checking out the source, I tracked down this:
/* The value of LINK_SIZE determines the number of bytes used to store links
as offsets within the compiled regex. The default is 2, which allows for
compiled patterns up to 64K long. This covers the vast majority of cases.
However, PCRE can also be compiled to use 3 or 4 bytes instead. This allows
for longer patterns in extreme cases. On systems that support it,
"configure" can be used to override this default. */
#ifndef LINK_SIZE
#define LINK_SIZE 2
#endif
so if you want to compile from source, knock yourself out.
Limit string length with RegEx
Possibly you just want to anchor the string at the end using $
. Also can drop the +
after the numeric quantifier:
^(?:0\d{9}|9\d{8})$
See test at regex101.com
PHP shortest/longest string length in array
Seems like you should use an array_map()
// Convert array to an array of string lengths
$lengths = array_map('strlen', $data);
// Show min and max string length
echo "The shortest is " . min($lengths) .
". The longest is " . max($lengths);
Note that the $lengths
array is unsorted, so you can easily retrieve the corresponding number for each string length.
Related Topics
PHP Replacing Multiple Spaces With a Single Space
Run a PHP File in a Cron Job Using Cpanel
Convert/Cast an Stdclass Object to Another Class
PHP Output Showing Little Black Diamonds With a Question Mark
JavaScript Equivalent of PHP'S In_Array()
How to Create and Download a CSV File from PHP Script
How to Show Ajax Requests in Url
How to Get Const'S Defined on a PHP Class
Why Does PHP 5.2+ Disallow Abstract Static Class Methods
MySQL: Select Random Entry, But Weight Towards Certain Entries
How to Rely on PHP PHP.Ini Precision Workaround For Floating Point Issue
Escaping Single Quote in PHP When Inserting into MySQL
How to Round Up a Number to Nearest 10
How to Get Code Point Number For a Given Character in a Utf-8 String