Question

I relogin to my server in dreamhost and test some scripts.And I found I couldn't use str_split. Message of Undefined function was given.I checked the version in the server and its PHP Version is 5.2.12.And I just wonder which version is required?Thanks.

Testcode:

<?php
$arr = str_split("lsdjflsdjflsdjflsdjfl");
print_r($arr);
?>

Message:

Fatal error: Call to undefined function:  str_split() in /test.php on line 3

Edit @Justin Johnson

I checked the server's system directory,and I found there are two versions of PHP in Dreamhost.In user's webroot,file will be parsed by PHP5 and that's why I got php 5.2.12 by putting a phpinfo.php in the webroot.And if php files are ran in command line directly using php test.php,another php version which is 4.x worked.That's the reason I got an error.When I use

/usr/local/php5/bin/php test.php

Everything is fine.

Was it helpful?

Solution

Rather than use str_split, it's usually much easier to iterate through the characters of the string directly:

$s="abc";
$i=0;
while(isset($s[$i])) {
    echo $s[$i++]." ";
}

see?

OTHER TIPS

First off: The PHP documentation will always say what version is required for every function on that function's documentation page directly under the function name.

It is possible that an .htaccess file is somewhere in your path and is causing a previous version (<5) of PHP to be used. To double (or triple) check to make sure that you are running in the proper PHP version, place this code above the line where you call str_split

echo "version:", phpversion(), 
    "<br/>\nstr_split exists? ", 
    function_exists("str_split") ? "true" : "false";

However, as shown by Col. Shrapnel, it is not necessary to convert a string to an array of individual characters in order to iterate over the characters of that string. Strings can also be iterated over using traditional iteration methods, thus making the call to str_split unnecessary and wasteful (unless you need to segment the string into fixed length chunks, e.g.: str_split($s, 3))

foreach ( str_split($s) as $c ) { 
    // do something with character $c
}

can be replaced by

$s = "lsdjflsdjflsdjflsdjfl";

for ( $i=0; isset($s[$i]); ++$i ) {
    // do something with character $s[$i]." ";
}

which is equally, if not more clear.

According to dreamhost wiki, you need to switch to php5 manually from control panel, if you created your domain before 2008 sept.

http://wiki.dreamhost.com/Installing_PHP5#Using_DreamHost.27s_PHP_5

PHP 5 was added to all plans by DreamHost as of June 2005. As of September 2008, support for PHP4 was discontinued, so you can no longer switch back to PHP 4 from PHP 5 from the panel.

If you haven't switched to PHP 5 yet, you can do this in the Control Panel. But, again, you will not be able to switch back to PHP 4 after switching to PHP 5.

Here's how to switch from PHP 4 to PHP 5:

  1. Log into the DreamHost Control Panel.
  2. Click Domains, then Manage Domains.
  3. Click the wrench icon next to the domain you want to activate PHP 5 on (under the Web Hosting column).
  4. Select PHP 5.x.x from the dropdown menu.
  5. Click Change fully hosted settings now! at the bottom of the section.
  6. Repeat steps 3-5 for each additional domain you want to activate.

you could also check your php version with

<?php  
   phpinfo();
?>

The version required is PHP 5 or later. So theoretically your program should work.

If you can't get str_split to work, just use a string as an array:

$stuff = "abcdefghijkl";
echo $stuff[3];

will produce

d

This method is fastest, anyway. I don't know if it suits your needs, but if it does, I hope it helps!

Could be anything in your code. How do we know its not a 10 line script or 2000 line script?

You can use preg_split() to split an array into single characters, but it will return an extra empty string at the begining and the end.

$a = preg_split("//","abcdefg");
echo json_encode($a);

prints:

["","a","b","c","d","e","f","g",""]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top