"Function split() is deprecated" in PHP? - php

$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);
}
}

Related

Deprecated preg_replace in 5.5.3

I am currently re-programming a framework in php and I upgraded our development server to php 5.5.3. When I fire up the webbrowser, it returns the following error:
[19-Oct-2013 16:54:05 Europe/Amsterdam] PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /Applications/MAMP/htdocs/fw/lib/lang.php on line 57
And line 57 is;
$response = preg_replace('/\{'.$find.'(:(.*))?\}/Ue', 'str_replace("{value}", "\\2", $replace)', $response);
I am really horrible at reading these php documentation, I am a beginner and simply changing preg_replace() by preg_replace_callback() is too good to be true. A co-worker told me it had to be something like $value[1] but that didn't work eather.
Is there a simple solution, am I overlooking something?
Here is the page about modifiers, giving you more detail about why it is deprecated, and what that means exactly.
Basically the reason is that the /e modifier causes a string to be evaluated as PHP code, as if eval was called. Using preg_replace_callback instead, which allows you to pass it an actual PHP function is indeed the way to go.
If you replace the string-code (second parameter) by an anonymous function, your code should then look something like this:
$response = preg_replace_callback(
'/\{'.$find.'(:(.*))?\}/U',
function($m) use ($replace) {
return str_replace($m, "\\2", $replace);
} ,
$response);
The keyword use makes sure that the anonymous function can use the $replace variable which should be defined in the calling scope. See this discussion.

Errors concerning depreciated eregi

This is the error i am receiving
Deprecated: Function eregi() is deprecated in /home/socia125/public_html/profile.php on
line231
This is my code
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match) {
// Find a match
if (eregi($Match, $agent)) {
break;
any help is appreciated
Well, as the manual quotes,
This function has been DEPRECATED as of PHP 5.3.0. Relying on this
feature is highly discouraged.
Use preg_match() instead. Be a bit careful though, because you cannot convert your search pattern 1:1 from eregi to preg_match().
Example to show the difference:
$t = "this is a test...";
if (preg_match("/test/i", $t)) echo "match!";
if (eregi('test', $t)) echo "match!";
There is a whole chapter in the php manual dedicated to the PCRE syntax.
But if you're just simply trying to find a string, use something like strstr or stristr, those are faster and easier to use.
Use preg_match instead of eregi.
But it seems like all it does is searching for a string, so you might be able to use stripos instead.
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match) {
// Find a match
if (stripos($Match, $agent) !== FALSE ) {
break;
I can't guarantee that it works as it should, since we don't see all of the code (especially contents and example values of $Match and $agent)
eregi() is deprecated as of 5.3.
You may ignore this as it is just a warning.
I would recommend to use preg_match() instead.
See the link here

php explode / split - co-existence in single script

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

How to handle deprecated function in PHP

I'm new in this site. I want to ask about PHP programming. How do we can handle deprecated function in PHP. I want to redirect it to my new function. As we know, ereg function has been deprecated in PHP 5.3.0 and recommended to preg_match (posix to PCRE). But, when we wrote a lot of code with ereg function, do we have to change it manually? I want a solution like this.
function ereg($pattern, $string, &$array) { return preg_match('#'.$pattern.'#', $string, $array); }
The main problem is not the ereg function, but solution of handling deprecated function.
I've been searching in Google. Someone suggest to use override_function (using APD extension). But, this extension is hard to find (I need precompiled extension build for windows). Anyone can help me?
I'm sorry for my bad English. I hope you can understand.
The reason they tell you it is deprecated, and they don't remove it completely, is to give you time to update your code.
If you don't want to update your code, you can always just not upgrade your install of PHP. Or you can wait until a release of PHP is out were ereg() is removed completely, and use your above solution.
Other possible solutions include doing a search/replace for all ereg calls, and replacing it my_ereg, which could be the function you defined above.
Also:
if(!function_exists("ereg")){ .... }
Define the function inside of the if statement that checks if the function already exists. This will make the transition smoother.
But all in all, the purpose of deprecation is to give developers time to update their code and stop using all of the deprecated functions before they remove it completely from the code base.
I believe some call it 'Maintenance'.
You could always use the function_exists function.
if(!function_exists('ereg'))
{
function ereg($pattern, $string, &$array)
{
return preg_match('#'.$pattern.'#', $string, $array);
}
}
Using this method would allow it to work in all version as if it is deprecated but still able to be used it will use the function but once it has been removed from php it will be able to use your user defined function.

change date format in php

i develop a webpage , in that i need to change the date format from 22/01/2010 to 2010-01-22
i use the following function but i am getting a warning as "Deprecated : Function ereg() is depreceted in c:\wamp\www\testpage.php on line 33" . Is there anyway to hide that error or is there any other way to change the date format ? Please help me to solve this issue .
Thanks in advance .
$datedue = $_REQUEST['txtJoiningdate'];
$r = ereg ("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})", $datedue, $redgs);
$billdate=$redgs[3]."-".$redgs[2]."-".$redgs[1];
Why not use strtotime,date and str_replace functions native to php to do the trick in one simple line?
This way you could easily change the format of the date to whatever you want easily using the many options date offers.
echo date('Y-m-d',strtotime(str_replace("/",".","22/01/2010")));
Outputs 2010-01-22
Documentation for functions used:
strtotime
date
str_replace
You are using deprecated functions. Use the preg_match instead. Also the call to preg_match should be in a if test.
<?php
$datedue = '22/01/2010';
if(preg_match('#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})#', $datedue, $redgs)) {
$billdate=$redgs[3]."-".$redgs[2]."-".$redgs[1];
echo $billdate; // prints 2010-01-22
}
?>
Use the PCRE functions preg_match or preg_replace instead:
$billdate = preg_replace('~([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})~', '$3-$2-$1', $datedue);
But you can also use a combination of explode, array_reverse and implode:
$billdate = implode('-', array_reverse(explode('/', $datedue)));
With recent versions of PHP, POSIX regex functions are indeed deprecated -- you should stop using them, and use the preg_* functions instead.
Here's your code, rewritten to use preg_match :
preg_match("#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})#", '22/01/2010', $redgs);
$billdate=$redgs[3]."-".$redgs[2]."-".$redgs[1];
var_dump($billdate);
And you'll get :
$ /usr/local/php-5.3/bin/php temp.php
string(10) "2010-01-22"
To be more precise, quoting the documentation of ereg :
This function has been DEPRECATED
as of PHP 5.3.0 and REMOVED as of
PHP 6.0.0. Relying on this feature is
highly discouraged.
So, don't hesitate to read the documentation of Regular Expressions (Perl-Compatible) -- which are more powerfull, faster, ... than the POSIX ones.
This should do it:
list($d, $m, $y) = explode('/', $datedue);
$billdate = date('Y-m-d', mktime(0,0,0,$m,$d,$y);
Or, this can be without the date functions as Gumbo suggested:
list($d, $m, $y) = explode('/', $datedue);
$billdate = "$y-$m-$d";
I'd recommend using the date though if you suspect you need to change the format in future. There is no need to use a regular expression for a simple splitting like that. Explode will be a lot faster in this case.
The ereg_ regular expression functions are deprecated as of PHP 5.3.0 and will be removed in PHP 6. For regular expressions, use the preg_ functions.
About hiding the error; you should never hide notices when developing as they help you build better code. Without that notice, you'd have happily used ereg and your application would have broken horribly when the server is updated to PHP 6. But, you can control the amount of shown errors with error_reporting(). Turning error_reporting off when your site goes live might be a good idea.
BTW, start accepting some answers if you find them helpful.
<?php
list($day, $month, $year) = split('/', $_REQUEST['txtJoiningdate']); // 22/01/2010
$new_date = "$year-$month-$day"; // $new_date now equals 2010-01-22
?>
$date = '01/24/2006';
echo date('Y-m-d', strtotime($date)); // outputs 2006-01-24
if ( ! function_exists('changeDateFormat'))
{
function changeDateFormat($original)
{
//$original = '2015-08-10';
// 2015-08-10 to 10-08-2015
$exploded = explode("-", $original);
$exploded = array_reverse($exploded);
$newFormat = implode("-", $exploded);
return $newFormat;
}
}

Categories