Need help fixing deprecated eregi errors

jullialynch
jullialynch used Ask the Experts™
on
I am updating my site to comply with php 5.3.0. I am just learning php.
How do I fix eregi errors that involve "images/"? I am not sure what delimiters to use or where to place them so that the images are still available.
Please look at the attached snippet of code and advise.
Thank you,
Julie
//Return the normal html if not SEO
	if (SEO_ENABLED != 'true' || !eregi("images/", $src)) {  //this only works on images in the images/ directory
				$image = '<img src="imagemagic.php?img='.$src.'&amp;w='.
				tep_output_string($width).'&h='.tep_output_string($height).'&amp;page='.$page.'"';
			} else {
				
				$src = str_replace("images/", "", $src);  //implied
				//return case sensitive extension
				if (eregi(".jpg", $src)) {
					$type=substr($src, stripos($src,".jpg"), 4);
				} elseif (eregi(".jpeg", $src)) {
					$type=substr($src, stripos($src,".jpeg"), 5);		
				} elseif (eregi(".gif", $src)) {
					$type=substr($src, stripos($src,".gif"), 4);		
				} elseif (eregi(".png", $src)) {
					$type=substr($src, stripos($src,".png"), 4);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
hi,

please provide images/ complete path like d:/......or /var/.....

so that we can trap if there is any issue.
SEO_ENABLED ??
The new function set instead of "eregi" is "preg_match" and you should be able to implement that easy by just replacing all the "eregi" (http://php.net/manual/en/function.eregi.php) with "preg_match" (http://www.php.net/manual/en/function.preg-match.php) however your code doesnt do any complete REGEX tests. So you might as well use just strpos() after this comment ill post again with your code changed over to use "strpos" (http://php.net/manual/en/function.strpos.php) instead.

Author

Commented:
Yes, SEO is enabled.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Author

Commented:
I have WAMP installed. Complete path is
C:/wamp/www/languageimages/images
About the errors you get. This is because "/" is a special symbol in REGEX and should be escaped. To escape special symbols put a \ infront of it. So instead of "images/" do "images\/"
//Return the normal html if not SEO 
        if (SEO_ENABLED != 'true' || !strpos($src, "images/")) {  //this only works on images in the images/ directory 
                                $image = '<img src="imagemagic.php?img='.$src.'&amp;w='. 
                                tep_output_string($width).'&h='.tep_output_string($height).'&amp;page='.$page.'"'; 
                        } else { 
                                 
                                $src = str_replace("images/", "", $src);  //implied 
                                //return case sensitive extension 
                                if (strpos($src, ".jpg")) { 
                                        $type=substr($src, stripos($src,".jpg"), 4); 
                                } elseif (strpos($src, ".jpeg")) { 
                                        $type=substr($src, stripos($src,".jpeg"), 5);            
                                } elseif (strpos($src, ".gif")) { 
                                        $type=substr($src, stripos($src,".gif"), 4);             
                                } elseif (strpos($src, ".png")) { 
                                        $type=substr($src, stripos($src,".png"), 4);

Open in new window

The attached code i have put up in the comment before this one of mine is what I would recommend for you to use. The eregi is a REGEX function and takes longer to run than just a strpos() function. Also REGEX is picky about the values you enter into the fields which is why you are getting that error about the /

Author

Commented:
It worked! I cannot tell you how long I have been struggling with this. Thank you so much.
Julie

Author

Commented:
Excellent. Easy to understand and a life saver.
No worries :), glad to have helped. Good luck on your future PHP scripting!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial