JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
use preg instead of ereg
authorJason Woofenden <jason@jasonwoof.com>
Tue, 26 May 2015 17:51:05 +0000 (13:51 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 26 May 2015 17:51:05 +0000 (13:51 -0400)
13 files changed:
email.php
encode.php
fdb.php
format.php
http.php
main.php
metaform.php
misc.php
session.php
tar.php
time.php
upload.php
uploader.php

index 65c71c3..4f77bb0 100644 (file)
--- a/email.php
+++ b/email.php
@@ -41,7 +41,7 @@ function email($from, $to, $subject, $message, $reply_to = '', $cc = '', $bcc =
        if($to   == '') { return 2; }
 
        #FIXME should allow many more characters here
-       $subject = ereg_replace("[^a-zA-Z0-9 _/#'.:&,-]", '_', $subject);
+       $subject = preg_replace("|[^a-z0-9 _/#'.:&,-]|i", '_', $subject);
 
        $headers = "From: $from";
        if($reply_to) {
@@ -74,7 +74,7 @@ function email_header($addr) {
                return '';
        }
 
-       if(ereg('<.*>$', $addr) !== false) {
+       if(preg_match('|<.*>$|', $addr) === 1) {
                # format 2
                $div = strrpos($addr, '<');
                $name = substr($addr, 0 , $div);
@@ -82,7 +82,7 @@ function email_header($addr) {
                $email = substr($addr, $div + 1, -1);
        } else {
                $email = $addr;
-               $name = ereg_replace('@.*', '', $addr);
+               $name = preg_replace('|@.*|', '', $addr);
        }
 
        if(!valid_email($email)) {
@@ -90,7 +90,7 @@ function email_header($addr) {
        }
 
        #FIXME should allow many more characters here
-       $name = ereg_replace("[^a-zA-Z0-9 _/'.-]", '_', $name);
+       $name = preg_replace("|[^a-z0-9 _/|i'.-]", '_', $name);
 
        return $name . ' <' . $email . '>';
 }
@@ -99,5 +99,5 @@ function email_header($addr) {
 
 # return true if e-mail is formatted like a valid email address
 function valid_email($email) {
-       return ereg('^[0-9a-zA-Z_~.+-]+@[0-9a-zA-Z.-]+\.[a-z]+$', $email) !== false;
+       return preg_match('|^[0-9a-zA-Z_~.+-]+@[0-9a-zA-Z.-]+\.[a-z]+$|', $email) === 1;
 }
index ab16a84..c09de2a 100644 (file)
@@ -437,34 +437,19 @@ function enc_evenodd($values, $name) {
        }
 }
 
-function enc_image_src($str) {
-       list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
-       return $src;
-}
-
-function enc_image_width($str) {
-       list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
-       return $width;
-}
-
-function enc_image_height($str) {
-       list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
-       return $height;
-}
-
-function enc_thumb_src($str) {
-       list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
-       return $src;
-}
-
-function enc_thumb_width($str) {
-       list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
-       return $width;
-}
-function enc_thumb_height($str) {
-       list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
-       return $height;
-}
+function wfpl_nth_word($str, $n) {
+       $a = explode(' ', $str);
+       return isset($a[$n]) ? $a[$n] : null;
+}
+
+# encoding is a space separated list of:
+# image_filename width height thumb_filename thumb_width thumb_height
+function enc_image_src($str) { wfpl_nth_word($str, 0); }
+function enc_image_width($str) { wfpl_nth_word($str, 1); }
+function enc_image_height($str) { wfpl_nth_word($str, 2); }
+function enc_thumb_src($str) { wfpl_nth_word($str, 3); }
+function enc_thumb_width($str) { wfpl_nth_word($str, 4); }
+function enc_thumb_height($str) { wfpl_nth_word($str, 5); }
 
 # example template: Length: ~length html~ day~length s~
 function enc_s($str) {
diff --git a/fdb.php b/fdb.php
index 3716ea6..5550464 100644 (file)
--- a/fdb.php
+++ b/fdb.php
@@ -46,8 +46,8 @@ function fdb_get_dir() {
 
 
 function fdb_fix_key($key) {
-       $key = ereg_replace('[^a-z0-9.-]', '_', strtolower($key));
-       $key = ereg_replace('^[-.]', '_', strtolower($key));
+       $key = preg_replace('|[^a-z0-9.-]|', '_', strtolower($key));
+       $key = preg_replace('|^[-.]|', '_', strtolower($key));
        return substr($key, 0, 32);
 }
 
index 81878eb..99283e1 100644 (file)
@@ -42,21 +42,21 @@ function format_options($str, $name) {
 }
 
 function format_int($str) {
-       $str = ereg_replace('[^0-9]', '', $str);
-       return ereg_replace('^0*([0-9])', '\1', $str);
+       $str = preg_replace('|[^0-9]|', '', $str);
+       return preg_replace('|^0*([0-9])|', '\1', $str);
 }
 
 # format the digits after the decimal point
 function format_decimals($str) {
-       $str = ereg_replace('[^0-9]', '', $str);
+       $str = preg_replace('|[^0-9]|', '', $str);
        if(strlen($str)) {
-               $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
+               $str = substr($str, 0, 1) . preg_replace('|0*$|', '', substr($str, 1));
        }
        return $str;
 }
 
 function _format_positive_decimal($str) {
-       $str = ereg_replace('[^0-9.]', '', $str);
+       $str = preg_replace('|[^0-9.]|', '', $str);
        $pos = strpos($str, '.');
        if($pos !== false) {
                $str = str_replace('.', '', $str);
@@ -80,7 +80,7 @@ function format_positive_decimal($str) {
 }
 
 function format_decimal($str) {
-       $str = ereg_replace('[^0-9.-]', '', $str);
+       $str = preg_replace('|[^0-9.-]|', '', $str);
        if(substr($str, 0, 1) == '-') {
                $str = format_positive_decimal(substr($str, 1));
                if($str !== '' && $str !== '0') {
@@ -103,7 +103,7 @@ function format_int_0($str) {
 
 # USA zip codes
 function format_zip($str) {
-       $str = ereg_replace('[^0-9]', '', $str);
+       $str = preg_replace('|[^0-9]|', '', $str);
        if(strlen($str) > 5) {
                return substr($str, 0, 5) . '-' . substr($str, 5);
        }
@@ -114,20 +114,20 @@ function format_filename($str, $allow_uppercase = false) {
        if(!$allow_uppercase) {
                $str = strtolower($str);
        }
-       $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
-       return ereg_replace('^[.-]', '_', $str);
+       $str = preg_replace('|[^a-z0-9_.-]|i', '_', $str);
+       return preg_replace('|^[.-]|', '_', $str);
 }
 
 function format_path($str, $allow_uppercase = false) {
        if(!$allow_uppercase) {
                $str = strtolower($str);
        }
-       $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
-       return ereg_replace('^[.-]', '_', $str);
+       $str = preg_replace('|[^a-z0-9_./-]|i', '_', $str);
+       return preg_replace('|^[.-]|', '_', $str);
 }
 
 function client_path_to_filename($path) {
-       $filename = ereg_replace(".*[:/\\]", '', $path);
+       $filename = preg_replace("|.*[:/\\\\]|", '', $path);
        return format_filename($filename, true);
 }
 
@@ -352,11 +352,11 @@ function format_hours($str) {
 # takes eg 12:23am
 # returns decimal number of hours since midnight
 function format_12hr_to_hours($str) {
-       if(eregi('noon', $str)) {
+       if(preg_match('|noon|i', $str) === 1) {
                return 12;
        }
        $hours = format_hours($str);
-       if($hours < 12 && eregi('p', $str)) {
+       if($hours < 12 && preg_match('|p|i', $str) === 1) {
                $hours += 12;
        }
        return $hours;
@@ -364,8 +364,8 @@ function format_12hr_to_hours($str) {
 
 
 function format_phone($str) {
-       $str = ereg_replace('[^0-9]', '', $str);
-       $str = ereg_replace('^1*', '', $str);
+       $str = preg_replace('|[^0-9]|', '', $str);
+       $str = preg_replace('|^1*|', '', $str);
        $len = strlen($str);
        $output = '';
 
index 51eb4d3..6c8f916 100644 (file)
--- a/http.php
+++ b/http.php
@@ -78,7 +78,7 @@ function redirect($url, $status = '302 Moved Temporarily', $message = '') {
                if(substr($url, 0, 1) == '/') {
                        $url = this_url_sans_path() . $url;
                } else {
-                       $url = ereg_replace('/[^/]*$', "/$url", this_url());
+                       $url = preg_replace('|/[^/]*$|', "/$url", this_url());
                }
        }
                        
index 3d4a751..eae4de9 100644 (file)
--- a/main.php
+++ b/main.php
@@ -40,7 +40,7 @@ function wfpl_main($dest = false) {
 
                # if it starts with './' then it's a relative URL, redirect
                if(substr($dest, 0, 2) == './') {
-                       redirect(ereg_replace('/[^/]*$', substr($dest, 1), this_url()));
+                       redirect(preg_replace('|/[^/]*$|', substr($dest, 1), this_url()));
                        exit();
                }
 
@@ -49,8 +49,8 @@ function wfpl_main($dest = false) {
 
        } else { # no dest arg
                $basename = $_SERVER['REDIRECT_URL'];
-               $basename = ereg_replace('.*/', '', $basename);
-               $basename = ereg_replace('\.html?$', '', $basename);
+               $basename = preg_replace('|.*/|', '', $basename);
+               $basename = preg_replace('|\.html?$|', '', $basename);
                if($basename == '') {
                        $basename = 'index';
                }
index a0dce78..f8112aa 100644 (file)
@@ -251,7 +251,7 @@ function get_fields() {
 
 # this one, that you're using to create forms
 function set_form_action() {
-       $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
+       $action = preg_replace('|.*/|', '', $_SERVER['REQUEST_URI']);
        if($action == '') $action = './';
        tem_set('form_action', $action);
 }
@@ -552,9 +552,9 @@ function make_php() {
 # make a URL for the edit page with all the fields filled in
 function edit_url() {
        $url = this_url();
-       $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
-       $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
-       $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
+       $url = preg_replace('|view_php=[^&]*|', 'edit=yes', $url);
+       $url = preg_replace('|download_tar=[^&]*|', 'edit=yes', $url);
+       $url = preg_replace('|/[a-z0-9_.]*\?|', '/?', $url);
        return $url;
 }
 
@@ -611,7 +611,7 @@ function preview() {
        }
        $preview = $preview_tem->run();
        unset($preview_tem);
-       $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
+       $preview = preg_replace('|type="submit"|', 'type="submit" disabled="disabled"', $preview);
        tem_set('preview', $preview);
        tem_show('hiddens');
        set_form_action();
index 528053c..de42d62 100644 (file)
--- a/misc.php
+++ b/misc.php
@@ -119,7 +119,7 @@ function this_year() {
 
 # return the number of the current month (1..12)
 function this_month() {
-       return ereg_replace('^0', '', strftime('%m'));
+       return preg_replace('|^0|', '', strftime('%m'));
 }
 
 # return today's date in yyyy-mm-dd format
index d75e07e..f31085f 100644 (file)
@@ -149,7 +149,7 @@ function session_exists() {
                return false;
        }
 
-       $session_key = ereg_replace('[^a-zA-Z0-9]', '', $_COOKIE['session_key']);
+       $session_key = preg_replace('|[^a-z0-9]|i', '', $_COOKIE['session_key']);
 
        if(!strlen($session_key) == 16) {
                return false;
diff --git a/tar.php b/tar.php
index bba514e..de9f47e 100644 (file)
--- a/tar.php
+++ b/tar.php
@@ -51,7 +51,7 @@ function make_tar($dirname, $files, $pre_archive_func = false, $tmpdir = false)
        $tmpdir .= '/' . sprintf('%08x%08x', mt_rand(), mt_rand());
        mkdir($tmpdir);
 
-       $dirname = ereg_replace('[^a-z0-9_-]', '', $dirname);
+       $dirname = preg_replace('|[^a-z0-9_-]|', '', $dirname);
        if($dirname == '') $dirname = 'foo';
        mkdir("$tmpdir/$dirname");
        foreach($files as $filename => $file_data) {
@@ -61,12 +61,12 @@ function make_tar($dirname, $files, $pre_archive_func = false, $tmpdir = false)
                } else {
                        $link = false;
                }
-               $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
+               $filename_fixed = preg_replace('|[^a-z0-9_.-]|i', '', $filename);
                if($filename != $filename_fixed) {
                        die("Invalid filename for tar archive");
                }
                if($link) {
-                       $target = ereg_replace('[^a-zA-Z0-9_./-]', '', $file_data);
+                       $target = preg_replace('|[^a-z0-9_./-]|i', '', $file_data);
                        system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
                } else {
                        write_file("$tmpdir/$dirname/$filename", $file_data);
index 3c42f79..f71d22a 100644 (file)
--- a/time.php
+++ b/time.php
@@ -76,8 +76,8 @@ function clean_ymd($year, $month, $day) {
 # pass date like 3/21/99
 # returns array(year, month, day)
 function mdy_clean($date) {
-       $date = ereg_replace('[^0-9/-]', '', $date);
-       $date = ereg_replace('-', '/', $date);
+       $date = preg_replace('|[^0-9/-]|', '', $date);
+       $date = preg_replace('|-|', '/', $date);
        $parts = explode('/', $date);
        switch(count($parts)) {
                case 1:
@@ -108,8 +108,8 @@ function mdy_to_ymd($date) {
 # pass date like 2008-11-21
 # returns array(year, month, day)
 function ymd_clean($date) {
-       $date = ereg_replace('[^0-9/-]', '', $date);
-       $date = ereg_replace('/', '-', $date);
+       $date = preg_replace('|[^0-9/-]|', '', $date);
+       $date = preg_replace('|/|', '-', $date);
        $parts = explode('-', $date);
        switch(count($parts)) {
                case 1:
index 8beca1c..e9ff5db 100644 (file)
@@ -111,10 +111,10 @@ function generate_filename($path, $mime = 'text/plain') {
        }
 
        # replace symbols with underscores
-       $filename = ereg_replace('[^a-z0-9_.]', '_', $filename);
+       $filename = preg_replace('|[^a-z0-9_.]|', '_', $filename);
 
        # remove dots from the beginning (no invisible files)
-       $filename = ereg_replace('^\.*', '', $filename);
+       $filename = preg_replace('|^\.*|', '', $filename);
 
        if(strlen($filename > 80)) {
                $filename = substr($filename, -80);
@@ -181,8 +181,8 @@ function save_uploaded_file($key, $path) {
 # standard places (like /usr/bin or /usr/local bin) and PHP's PATH environment
 # variable is not set appropriately.
 function path_to($prog, $or_die = true) {
-       $prog = ereg_replace('[^a-zA-Z0-9_.-]', '', $prog);
-       $prog = ereg_replace('^[-.]*', '', $prog);
+       $prog = preg_replace('|[^a-z0-9_.-]|i', '', $prog);
+       $prog = preg_replace('|^[-.]*|', '', $prog);
        if($prog == '') {
                die('Invalid argument to path_to()');
        }
@@ -258,7 +258,7 @@ function make_thumbnail($filename, $max_width = '70', $max_height = '70') {
 function exec_or_die($command, $doing_what) {
        exec($command, $dummy, $ret);
        if($ret != 0) {
-               $base = basename(ereg_replace(' .*', '', $command));
+               $base = basename(preg_replace('| .*|', '', $command));
                die("$doing_what failed. $base called exit($ret)");
        }
 }
@@ -280,7 +280,7 @@ function imagemagick_mogrify($in_filename, $args, $doing_what = "Image conversio
 }
 
 function format_int_70($str) {
-       $str = ereg_replace('[^0-9]', '', $str);
+       $str = preg_replace('|[^0-9]|', '', $str);
        if($str == '') {
                $str = '70';
        }
@@ -352,7 +352,7 @@ function image_w_h_or_die($filename) {
 # and names, call convert_uploaded_image().
 function save_uploaded_image($key, $path, $image_width = 0, $image_height = 0, $thumbnail_width = 0, $thumbnail_height = 0) {
      $image_w_h_thumb_w_h = convert_uploaded_image($key, $path, $image_width, $image_height, $thumbnail_width, $thumbnail_height);
-     return ereg_replace(' .*', '', $image_w_h_thumb_w_h);
+     return preg_replace('| .*|', '', $image_w_h_thumb_w_h);
 }
 
 function ext_to_web_image_ext($in) {
index 0badb1a..e661882 100644 (file)
@@ -94,8 +94,8 @@ function wfpl_uploader_progress() {
 
        $file = $_REQUEST['wfpl_upload_progress'];
        $file = strtolower($file);
-       $file = ereg_replace('[^a-z0-9.-]', '_', $file);
-       $file = ereg_replace('^[.-]', '_', $file);
+       $file = preg_replace('|[^a-z0-9.-]|', '_', $file);
+       $file = preg_replace('|^[.-]|', '_', $file);
        $file = $GLOBALS['wfpl_uploader_path'] . "/progress/$file";
        
        $waited = 0;