Jump to content


Corporate Sponsors


Latest News: (loading..)

- - - - -

Function ereg_replace() is deprecated


127 replies to this topic

#1 ianric

  • Community Member
  • 373 posts
  • Real Name:Ian Richardson
  • Gender:Male
  • Location:Manchester, UK

Posted 28 July 2009, 17:01

Hi

Just testing my site locally with Wampserver and PHP 5.3 and I'm getting the following error when viewing a product, product_info.php

Deprecated: Function ereg_replace() is deprecated in C:\wamp\www\includes\functions\general.php on line 61

It's this function

  function tep_sanitize_string($string) {
	$string = ereg_replace(' +', ' ', trim($string));

	return preg_replace("/[<>]/", '_', $string);
  }

I Searched and managed to clear a similar error in the classes\language.php. That solution changed ereg to preg_match

I'm also getting the same error displayed on the index.php. Deprecated: Function ereg() is deprecated in C:\wamp\www\index.php on line 196, another ereg statement. It happens when I filter the product listing via the manufacturers drop down, shows results pages 1 to 5 navigation just below.


// We are asked to show only specific catgeory
		$listing_sql = "select " . $select_column_list . " p.products_date_added, p.products_id, p.manufacturers_id, m.manufacturers_name, p.products_price, p.products_media, p.products_label, p.products_catno, p.products_model, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['filter_id'] . "' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";
	  } else {
// We show them all
		$listing_sql = "select " . $select_column_list . " p.products_date_added, p.products_id, p.manufacturers_id, m.manufacturers_name, p.products_price, p.products_media, p.products_label, p.products_catno, p.products_model, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";
	  }
	}

This is line 196	if ( (!isset($HTTP_GET_VARS['sort'])) || (!ereg('^[1-8][ad]$', $HTTP_GET_VARS['sort'])) || (substr($HTTP_GET_VARS['sort'], 0, 1) > sizeof

($column_list)) ) {
	  for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
		if ($column_list[$i] == 'PRODUCT_LIST_NAME') {
		  $HTTP_GET_VARS['sort'] = $i+1 . 'a';
		  $listing_sql .= " order by pd.products_name";
		  break;
		}
	  }
	} else... {

Please could someone hold my hand for a while. Any help will be much appreciated

Cheers

Ian

Edited by ianric, 28 July 2009, 17:02.


#2 sharma.atul85

  • Community Member
  • 156 posts
  • Real Name:Atul Sharma
  • Gender:Male
  • Location:Punjab, India

Posted 28 July 2009, 17:19

look yr search for google for this as ereg_replace() is not compatible with latest version of php.
its there in language.php file where this need to be changed .
thats what I remember now

#3 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 28 July 2009, 22:54

Try changing
  function tep_sanitize_string($string) {
	$string = ereg_replace(' +', ' ', trim($string));

	return preg_replace("/[<>]/", '_', $string);
  }
to
  function tep_sanitize_string($string) {
	$string = preg_replace('{ +}', ' ', trim($string));

	return preg_replace("/[<>]/", '_', $string);
  }
and see if that does what you want.
Always backup before making changes.

#4 ianric

  • Community Member
  • 373 posts
  • Real Name:Ian Richardson
  • Gender:Male
  • Location:Manchester, UK

Posted 29 July 2009, 19:56

View Postecartz, on Jul 28 2009, 11:54 PM, said:

Try changing
  function tep_sanitize_string($string) {
	$string = ereg_replace(' +', ' ', trim($string));

	return preg_replace("/[<>]/", '_', $string);
  }
to
  function tep_sanitize_string($string) {
	$string = preg_replace('{ +}', ' ', trim($string));

	return preg_replace("/[<>]/", '_', $string);
  }
and see if that does what you want.

Hi Ecartz

Thanks for that. It did the trick

Two down, one to go. Just need to do a bit of reading and try to figure out the same error in index.php

Just one more thing, is changing the ereg to preg_replace backwards compatible?

Thanks again

Ian

#5 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 29 July 2009, 23:15

View Postianric, on Jul 29 2009, 03:56 PM, said:

Just one more thing, is changing the ereg to preg_replace backwards compatible?
Yes. The preg_replace function has been standard in PHP since 4.2 (and existed earlier than that). You can read more about it at the documentation for PCRE on the php.net site.

In terms of osCommerce, the version that I suggested should be exactly equivalent to the original with ereg. The syntax is different but the functionality should be identical.

Some other ereg_replace calls that you might find:
ereg_replace('2037' . '$', $year, date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, 2037)))
ereg_replace('"', ' ', $pieces[$k])
ereg_replace('(' . implode('|', $from) . ')', $to, $string)
ereg_replace('[^0-9]', '', $number)
ereg_replace('-language', '-' . $languages[$j]['directory'], $cached_file)
ereg_replace('(' . implode('|', $from) . ')', $to, $string)
ereg_replace("\r","",$which_text)
ereg_replace('-language', '-' . $language, $cache_blocks[$i]['file'])
ereg_replace(",\n$", '', $schema)
ereg_replace("\n#", "\n".'\#', $row)
ereg_replace(', $', '', $schema)
would become
preg_replace('{2037\z}', $year, date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, 2037)))
str_replace('"', ' ', $pieces[$k])
preg_replace('{(' . implode('|', $from) . ')}', $to, $string)
preg_replace('{\D}', '', $number)
str_replace('-language', '-' . $languages[$j]['directory'], $cached_file)
str_replace("\r","",$which_text)
str_replace('-language', '-' . $language, $cache_blocks[$i]['file'])
preg_replace("{,\n\z}", '', $schema)
preg_replace("{\n#}", "\n".'\#', $row)
preg_replace('{, \z}', '', $schema)
Warning: I haven't tested any of these.
Always backup before making changes.

#6 toyicebear

  • Community Sponsor
  • 5,693 posts
  • Real Name:Nick
  • Location:World Citizen

Posted 30 July 2009, 05:21

These 3 are also deprecated from PHP5.3

ereg

eregi

eregi_replace
Basics for osC 2.2 Design - Basics for Design V2.3+ - Seo & Sef Url's - Meta Tags for Your osC Shop - Steps to prevent Fraud... - MS3 and Team News... - SEO, Meta Tags, SEF Urls and osCommerce

Check out my profile [click here] for information on professional services, custom coding, templates, SEO optimization, modifications, commercial support and help.

#7 tfoolen

  • Community Member
  • 45 posts
  • Real Name:Tom Foolen

Posted 11 August 2009, 08:34

View Posttoyicebear, on Jul 30 2009, 07:21 AM, said:

These 3 are also deprecated from PHP5.3

ereg

eregi

eregi_replace

to what has the EREG has to be changed. Damned, this XAMPP upgrade (PHP upgrade actually to 5.3) gives me a hell of a lot of errors (and work)

#8 spooks

  • Community Member
  • 7,017 posts
  • Real Name:Sam
  • Gender:Male
  • Location:UK

Posted 11 August 2009, 09:57

The list of effected functions is bigger than that, its due to the regex extension being deprecated

ereg_ replace
ereg
eregi_ replace
eregi
split
spliti
sql_ regcase
Sam

Remember, What you think I ment may not be what I thought I ment when I said it.

Contributions:


Auto Backup your Database, Easy way

Multi Images with Fancy Pop-ups, Easy way

Products in columns with multi buy etc etc

Disable any Category or Product, Easy way

Secure & Improve your account pages et al.

#9 spooks

  • Community Member
  • 7,017 posts
  • Real Name:Sam
  • Gender:Male
  • Location:UK

Posted 13 August 2009, 17:58

Replacing the deprecated SPLIT function

A question came up asking how to replace the split used in the tep_parse_search_string function (about 594 of general.php)

existing line:

$pieces = split('[[:space:]]+', $search_str);


replace with:

$pieces = preg_split("/[\s,]+/", $search_str);

This new code has the added advantage that it splits on commas too, which would cause failure on old code.

Edited by spooks, 13 August 2009, 18:08.

Sam

Remember, What you think I ment may not be what I thought I ment when I said it.

Contributions:


Auto Backup your Database, Easy way

Multi Images with Fancy Pop-ups, Easy way

Products in columns with multi buy etc etc

Disable any Category or Product, Easy way

Secure & Improve your account pages et al.

#10 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 15 August 2009, 05:00

View Posttfoolen, on Aug 11 2009, 04:34 AM, said:

to what has the EREG has to be changed.
ereg would go to preg_match or strstr or strpos, e.g.
	  if (ereg('->', $use_function)) {
would become
	  if (preg_match('{->}', $use_function)) {
or
	  if (strstr('->', $use_function) !== false) {
or
	  if (strpos('->', $use_function) !== false) {
For performance reasons, strpos would be preferred in that example. However, it won't always work. I can't find any examples of eregi or eregi_replace in the osCommerce code base, but you can add an i to the PCRE pattern to make it case insensitive, e.g. '{a}i' would match both a and A.
Always backup before making changes.

#11 Xpajun

  • Community Member
  • 1,272 posts
  • Real Name:Julian
  • Gender:Male
  • Location:UK

Posted 30 August 2009, 11:14

I'm hoping this may be the official ereg answer post - because it's currently doing my head in :huh:

I only have php5.3 on my computer and which I use for adding (add-ons etc) and testing my store on.

Anyway I've been updating ereg and eregi as they occur - well at some time or other we are going to get php6 on-line and we store owners need to be ready v3 may or may not be ready and even if it is it may or may not have the add-ons I want or accept the tweeks I require.

currently I've hit a problem where I can't load to the cart (only on my computer) I'm using the same file as my live shop (with the exception of both configure.php files)

The error I get on clicking add to cart is: Fatal error: Call to undefined function preg() in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 1094 which is:
		if (preg('/^[0-9]$/', $char)) $rand_value .= $char;
	  }
and was:
		if (ereg('^[0-9]$', $char)) $rand_value .= $char;
	  }
which gave this as an error:

Deprecated: Function ereg() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 1094

Deprecated: Function ereg() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 1094

Deprecated: Function ereg() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 1094

Deprecated: Function ereg() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 1094

Deprecated: Function ereg() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 1094

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php:1094) in /Applications/XAMPP/xamppfiles/htdocs/includes/functions/general.php on line 33

line 33 is:
	header('Location: ' . $url);

My question is (apart from help!) are we able to change the ereg as they crop up or do we have to find them all and change them at once?

#12 spooks

  • Community Member
  • 7,017 posts
  • Real Name:Sam
  • Gender:Male
  • Location:UK

Posted 30 August 2009, 11:44

As Matt pointed out, you would replace ereg with preg_match, preg does not exist!!

so
if (ereg('^[0-9]$', $char)) $rand_value .= $char;
becomes
if (preg_match('^[0-9]$', $char)) $rand_value .= $char;

Also as has been pointed out elsewhere, a work around (till v6 at least) is:

In includes/application_top.php and admin/includes/application_top.php

find:

 //set the level of error reporting
  error_reporting(E_ALL & ~E_NOTICE);

replace with:

//set the level of error reporting
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

Edited by spooks, 30 August 2009, 11:51.

Sam

Remember, What you think I ment may not be what I thought I ment when I said it.

Contributions:


Auto Backup your Database, Easy way

Multi Images with Fancy Pop-ups, Easy way

Products in columns with multi buy etc etc

Disable any Category or Product, Easy way

Secure & Improve your account pages et al.

#13 Xpajun

  • Community Member
  • 1,272 posts
  • Real Name:Julian
  • Gender:Male
  • Location:UK

Posted 30 August 2009, 12:35

Doh! :blush:

works now

Thank you spooks


The work around is ok at the moment but is going to pretty useless when php6 comes along which is why I've decided to update them

#14 simas12

  • Community Member
  • 2 posts
  • Real Name:simas

Posted 03 September 2009, 12:39

hi

i am new with OS Commerce

i have got this problem

on the top of the page there are
Deprecated: Function eregi() is deprecated in C:\Program Files\EasyPHP5.3.0\www\catalog\includes\classes\language.php on line 87

What does it mean and How can i solve this problem
Thanks.

#15 Xpajun

  • Community Member
  • 1,272 posts
  • Real Name:Julian
  • Gender:Male
  • Location:UK

Posted 03 September 2009, 15:09

Hi simas12 ,

As you are new to osCommerce your best bet may well be to do the fix Sam has describe here especially if you need to get a store running - it will last you until your server updates to php6 (mine is only running php 5.2.9 at the moment)

View Postspooks, on Aug 30 2009, 12:44 PM, said:

Also as has been pointed out elsewhere, a work around (till v6 at least) is:

In includes/application_top.php and admin/includes/application_top.php

find:

 //set the level of error reporting
  error_reporting(E_ALL & ~E_NOTICE);

replace with:

//set the level of error reporting
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);


#16 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 07 September 2009, 03:16

The unix diff from admin/includes/classes/phplot.php:
677,678c677,678
<					   $which_text = ereg_replace("\r","",$which_text);
<					   $str = split("\n",$which_text); //multiple lines submitted by Remi Ricard
---
>					   $which_text = str_replace("\r", '', $which_text);
>					   $str = explode("\n",$which_text); //multiple lines submitted by Remi Ricard
782c782
<			   if (eregi($asked, $accepted)) {
---
> 		 if ( preg_match('{' . $asked . '}i', $accepted) ) {
939c939
<			   $str = split("\n",$this->title_txt); 
---
>			   $str = explode("\n",$this->title_txt);
Note that three of these are examples of times when it is more appropriate not to use a regular expression function at all. For those who are not familiar with unix diff, the lines that start with < are the old lines and the lines that start with > are the replacements. The numbers are line numbers for the lines that follow, e.g. lines 677 and 678 in the original file are replaced by lines 677 and 678 in the new file. The greater than signs should not be pasted into the file.
Always backup before making changes.

#17 ianric

  • Community Member
  • 373 posts
  • Real Name:Ian Richardson
  • Gender:Male
  • Location:Manchester, UK

Posted 07 October 2009, 19:37

Hi

Thanks to everyone helping me out on this. At first it was very daunting but after reading the manual and Google, it's quite easy. I've decided to add to this thread so that it's all in one place for the future.

One edit that has me stumped is this code in admin/server_info.php

<?php
    ob_start();
    phpinfo();
    $phpinfo = ob_get_contents();
    ob_end_clean();

    $phpinfo = str_replace('border: 1px', '', $phpinfo);

*** This ereg statement here ***
    ereg('<body>(.*)</body>', $phpinfo, $regs);


    echo '<table border="1" cellpadding="3" width="600" style="border: 0px; border-color: #000000;">' .
         '  <tr><td><a href="http://www.oscommerce.com"><img border="0" src="images/oscommerce.png" title="' . PROJECT_VERSION . '" /></a><h1 class="p"> ' . PROJECT_VERSION . '</h1></td>' .
         '  </tr>' .
         '</table>';
    echo $regs[1];
  } else {
    phpinfo();
  }
?>

I can't figure this one out. Even examples I found on the net don't seem to work. If I replace with

preg_match('/<body>(.*)</body>/', $phpinfo, $regs);

I get "Warning: preg_match() [function.preg-match]: Unknown modifier 'b' in C:\xampp\Htdocs\admin\server_info.php on line 113"

I've tried | as a different modifer, (Google)?? Escaping the \/body cures the error messages but only the osc.png is displayed, not the whole info pages.

The other one is in admin/banner_manager.php

I've updated the file to PHP 5.3 but I get

"Warning: imagecolorexact(): 66 is not a valid Image resource in C:\xampp\Htdocs\admin\includes\classes\phplot.php on line 1305". The error is repeated 16 times with same and different line numbers. This is the function on line 1305

	function SetIndexColor($which_color) { //Color is passed in as anything
  		list ($r, $g, $B) = $this->SetRgbColor($which_color);  //Translate to RGB
		$index = ImageColorExact($this->img, $r, $g, $B);
		if ($index == -1) {
	  			//return ImageColorAllocate($this->img, $r, $g, $B);
	  			//return ImageColorClosest($this->img, $r, $g, $B);
	  			return ImageColorResolve($this->img, $r, $g, $B); //requires PHP 3.0.2 and later
 		} else {
	  			return $index;
  		}
	}

I *think* these are my last fixes so any help would be appreciated.

Many thanks again

Ian

#18 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 07 October 2009, 20:31

View Postianric, on 07 October 2009, 19:37, said:

preg_match('/<body>(.*)</body>/', $phpinfo, $regs);
Try
preg_match('{<body>(.*)</body>}xsmi', $phpinfo, $regs);
You may only need s or m, but it worked in my test with all of them and it's a Perl Best Practice to use xsm (along with the {} as the regex delimiters). The i covers a possible issue where you might have <BODY> rather than <body>.

In regards to your banner manager issue, are you using the same code changes that I proposed or different ones? If different ones, could you try the ones that I posted? If that doesn't work, please post what you are trying.

Incidentally, Mark Evans was working on a version of 2.2 without the deprecated functions. You might be able to get that on github.
Always backup before making changes.

#19 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 12 October 2009, 21:57

The github links:
Always backup before making changes.

#20 jlb922

  • Community Member
  • 5 posts
  • Real Name:Jeff

Posted 22 October 2009, 01:02

Can anyone help me fix this one in headertags.php?

$pageName = ucwords(ereg_replace("[^A-Za-z0-9]", " ", $pageName));

Thanks in advance!