I have php two servers with different versions of php,
and am having trouble with split statement which seems to be deprecated on new box.
I replaced with explode which is not known to old box.
$connect = explode(";", DB_CONNECT);
$connect = split(";", DB_CONNECT);
what statement(s) will make both servers happy?
Upgrading is not an option tonight.
A better option in the short term is to disable the warning until you're able to upgrade your PHP version.
See: http://php.net/manual/en/function.error-reporting.php
If explode doesnt exist, create it
if (!function_exists('explode')) {
function explode($str, $array) {
return split($str, $array);
}
}
Try preg_split() and preg_match_all(). The latter doesn't return an array, but may fill in an array pass in as third argument.
I have not tried this but hopefully it will work. Good luck.
function ultraExplode($del,$arr){
$ver=phpversion();
if ($ver>=5) return explode($del,$arr);
else return split($del,$arr);}
Related
I'm looking for the kind to access at the value of an array directly from the object's method.
For example :
With this syntax, no problem :
$myArray = $myObject->getArray();
print_r($myArray[0])
But to reduce the number of line in source code, how to get the element directly with the method ?
I do that, but it's not correct :
$myArray = $myObject->getArray()[0];
The following is only available for PHP 5.4 and supposedly higher.
$myArray = $myObject->getArray()[0];
Unfortunately there is no quicker way below PHP 5.4.
See #deceze's answer for a good alternative.
For PHP 5.3-:
$myArray = current($myObject->getArray());
or
list($myArray) = $myObject->getArray();
If you are on php 5.4 (which support array dereferencing) you can do the second option:
$myArray = $myObject->getArray()[0];
If you are on PHP < 5.4 you can "fix" it in the class (of which the object is a instance):
class Foo
{
public function getArray()
{
return $this->theArray;
}
public function getFirstItem()
{
return $this->theArray[0];
}
}
$myObject = new Foo();
print_r($myObject->getFirstItem());
But to reduce the number of line in source code, how to get the element directly with the method ?
Although it is possible to achieve this in PHP 5.4 with the syntax you've demonstrated, I have to ask, why would you want that? There are ways of doing it in 5.3 in a one-liner, but I don't see the need to do this. The number of lines is surely less interesting than the readability of the code?
IT IS IMPOSSIBRRUUU.
Serious answer:
sadly it is not possible. You can write a very ugly wrapper like this:
function getValue($arr, $index)
{
return $arr[$index];
}
$myArray = getValue($myObject->getArray(), 0);
But that makes less readable code.
read other answers about php 5.4 Finally!
hi i have many session values in my project and i use the syntext for session is
$_SESSION['username'] = $somevalue;
this things is implemented in may pages around 2000 pages. now i want to replace this thing to
$_SESSION['username'] = (string)$somevalue
in all the pages simultaneously. how can i do this in dreamwaver. please help me. there are many different session values used in my pages.
Is there any way to convert all session values into string simultaneosly.
i mean any regex method like $_SESSION[.] = (string) like this. or any other method. please tell me .
Thanks.
It depends of what version of PHP you have. For >=5.3, use Peter's version, for <5.3, use
function stringify($item)
{
return (string)$item;
}
$_SESSION = array_map('stringify', $_SESSION);
array_map function is probably what you are looking for:
$_SESSION = array_map(function($item) { return (string)$item; }, $_SESSION);
PHP 5.3 is required for anonymous function, in earlier versions you have to pass function name as first argument.
Just in case you want it in your 2000 code files instead of converting the values at runtime in your script: Don't know if Dreamweaver supports regex search and replace and what the backreference chars are. But try replacing this
\$_SESSION\['[^']+'\]\s*=\s*
with this:
$0(string)
The $0 is the backreference to the matched pattern. If that doesn't work, try \0 or \\0 instead.
This question already exists:
Closed 11 years ago.
Possible Duplicate:
Access array element from function call in php
instead of doing this:
$s=explode('.','hello.world.!');
echo $s[0]; //hello
I want to do something like this:
echo explode('.','hello.world.!')[0];
But doesn't work in PHP, how to do it? Is it possible? Thank you.
As the oneliners say, you'll have to wait for array dereferencing to be supported. A common workaround nowadays is to define a helper function for that d($array,0).
In your case you probably shouldn't be using the stupid explode in the first place. There are more appropriate string functions in PHP. If you just want the first part:
echo strtok("hello.world.!", ".");
Currently not but you could write a function for this purpose:
function arrayGetValue($array, $key = 0) {
return $array[$key];
}
echo arrayGetValue(explode('.','hello.world.!'), 0);
It's not pretty; but you can make use of the PHP Array functions to perform this action:
$input = "one,two,three";
// Extract the first element.
var_dump(current(explode(",", $input)));
// Extract an arbitrary element (index is the second argument {2}).
var_dump(current(array_slice(explode(",", $input), 2, 1)));
The use of array_slice() is pretty foul as it will allocate a second array which is wasteful.
Not at the moment, but it WILL be possible in later versions of PHP.
This will be possible in PHP 5.4, but for now you'll have to use some alternative syntax.
For example:
list($first) = explode('.','hello.world.!');
echo $first;
While it is not possible, you technically can do this to fetch the 0 element:
echo array_shift(explode('.','hello.world.!'));
This will throw a notice if error reporting E_STRICT is on.:
Strict standards: Only variables should be passed by reference
nope, not possible. PHP doesn't work like javascript.
No, this is not possible. I had similar question before, but it's not
$stringText = "[TEST-1] test task 1 Created: 06/Apr/11 Updated: 06/Apr/11";
$splitArray = split(" ",$stringText);
Deprecated: Function split() is deprecated in C:\wamp\www\RSS.php on line 27
Why this error happen ?
http://php.net/manual/en/function.split.php
From the manual
Warning This function has been
DEPRECATED as of PHP 5.3.0. Relying on
this feature is highly discouraged
Note:
As of PHP 5.3.0, the regex extension
is deprecated in favor of the PCRE
extension. Calling this function will
issue an E_DEPRECATED notice. See the
list of differences for help on
converting to PCRE.
I guess you're supposed to use the alternative preg_split(). Or if you're not using a regex, just use explode
split has been replaced with explode, see http://php.net/explode for more information. Works the same as split, but split is 'deprecated' basically means that is a old function that shouldn't be used anymore, and is not likely to be in later versions of php.
Use following explode function:
$command = explode(" ", $tag[1]);
This is the standard solution for this case.
Its Perfectly working.
Ahh, the docs says about it. And the docs also say which functions should be used instead of this:
preg_split
explode
str_split
Because the function has been deprecated? You can customize the error_reporting level to not log / display the depreciated errors. But it would be more prudent to just correct the issue (IE use explode instead for the simple split you are doing above.)
You can use this custom function for old codes:
if (!function_exists('split')) {
function split($pattern, $subject, $limit=-1, $flags=0){
return preg_split($pattern, $subject, $limit, $flags);
}
}
Why does this code not work?
echo explode("?", $_SERVER["REQUEST_URI"])[0];
It says syntax error, unexpected '['.
Oddly, this works:
$tmp = explode("?", $_SERVER["REQUEST_URI"]);
echo $tmp[0];
But I really want to avoid to create such a $tmp variable here.
How do I fix it?
After the helpful answers, some remaining questions: Is there any good reason of the design of the language to make this not possible? Or did the PHP implementors just not thought about this? Or was it for some reason difficult to make this possible?
Unlike Javascript, PHP can't address an array element after a function. You have to split it up into two statements or use array_slice().
This is only allowed in the development branch of PHP (it's a new feature called "array dereferencing"):
echo explode("?", $_SERVER["REQUEST_URI"])[0];
You can do this
list($noQs) = explode("?", $_SERVER["REQUEST_URI"]);
or use array_slice/temp variable, like stillstanding said. You shouldn't use array_shift, as it expects an argument passed by reference.
It's a (silly) limitation of the current PHP parser that your first example doesn't work. However, supposedly the next major version of PHP will fix this.
Take a look at this previous question.
Hoohaah's suggestion of using strstr() is quite nice. The following will return from the beginning of the string until the first ? (the third argument for strstr() is only available for PHP 5.3.0 onward):
echo strstr($_SERVER["REQUEST_URI"], "?", TRUE);
Or if you want to stick with explode, you can make use if list(). (The 2 indicates that at most 2 elements will be returned by explode).
list($url) = explode("?", $_SERVER["REQUEST_URI"], 2);
echo $url;
Finally, to use the natively available PHP URL parsing;
$info = parse_url($_SERVER["REQUEST_URI"]);
echo $info["scheme"] . "://" . $info["host"] . $info["path"];
EDIT:
echo current(explode("?", $_SERVER["REQUEST_URI"]));
I believe PHP 5.4 comes with this feature, at long last. Sadly, it will be a while before your average web host updates their PHP version. My Ubuntu VPS doesn't even have it in the default repos.