Replace ereg with preg_*

This commit is contained in:
AngryPHPNerd 2012-02-27 22:14:02 +01:00
parent edf6cd6d83
commit 0e2cbb0b88
21 changed files with 76 additions and 72 deletions

View File

@ -143,7 +143,7 @@ class ContentNegotiator {
$content = str_replace(' ',' ', $content);
$content = str_replace('<br>','<br />', $content);
$content = eregi_replace('(<img[^>]*[^/>])>','\\1/>', $content);
$content = preg_replace('#(<img[^>]*[^/>])>#i', '\\1/>', $content);
$response->setBody($content);
@ -170,14 +170,14 @@ class ContentNegotiator {
$content = preg_replace('/<base href="([^"]*)" \/>/',
'<base href="$1"><!--[if lte IE 6]></base><![endif]-->', $content);
$content = ereg_replace("<\\?xml[^>]+\\?>\n?",'',$content);
$content = preg_replace("#<\\?xml[^>]+\\?>\n?#", '', $content);
$content = str_replace(array('/>','xml:lang','application/xhtml+xml'),array('>','lang','text/html'), $content);
// Only replace the doctype in templates with the xml header
if($hasXMLHeader) {
$content = ereg_replace('<!DOCTYPE[^>]+>', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', $content);
$content = preg_replace('/<!DOCTYPE[^>]+>/', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', $content);
}
$content = ereg_replace('<html xmlns="[^"]+"','<html ', $content);
$content = preg_replace('/<html xmlns="[^"]+"/','<html ', $content);
$response->setBody($content);
}

View File

@ -465,7 +465,7 @@ class Director {
*/
static function makeRelative($url) {
// Allow for the accidental inclusion of a // in the URL
$url = ereg_replace('([^:])//','\\1/',$url);
$url = preg_replace('#([^:])//#', '\\1/', $url);
$url = trim($url);
// Only bother comparing the URL to the absolute version if $url looks like a URL.
@ -866,4 +866,4 @@ class Director {
return false;
}
}
}

View File

@ -38,7 +38,7 @@ class HTTP {
*/
static function absoluteURLs($html) {
$html = str_replace('$CurrentPageURL', $_SERVER['REQUEST_URI'], $html);
return HTTP::urlRewriter($html, '(substr($URL,0,1) == "/") ? ( Director::protocolAndHost() . $URL ) : ( (ereg("^[A-Za-z]+:", $URL)) ? $URL : Director::absoluteBaseURL() . $URL )' );
return HTTP::urlRewriter($html, '(substr($URL,0,1) == "/") ? ( Director::protocolAndHost() . $URL ) : ( (preg_match("/^[A-Za-z]+:/", $URL)) ? $URL : Director::absoluteBaseURL() . $URL )' );
}
/*

View File

@ -291,26 +291,25 @@ class Convert {
// Replace images with their alt tags
if($config['ReplaceImagesWithAlt']) {
$data = eregi_replace('<img[^>]*alt *= *"([^"]*)"[^>]*>', ' \\1 ', $data);
$data = eregi_replace('<img[^>]*alt *= *([^ ]*)[^>]*>', ' \\1 ', $data);
$data = preg_replace('/<img[^>]*alt *= *"([^"]*)"[^>]*>/i', ' \\1 ', $data);
$data = preg_replace('/<img[^>]*alt *= *([^ ]*)[^>]*>/i', ' \\1 ', $data);
}
// Compress whitespace
if($config['CompressWhitespace']) {
$data = ereg_replace("[\n\r\t ]+", " ", $data);
$data = preg_replace("/\s+/", " ", $data);
}
// Parse newline tags
$data = ereg_replace("[ \n\r\t]*<[Hh][1-6]([^A-Za-z0-9>][^>]*)?> *", "\n\n", $data);
$data = ereg_replace("[ \n\r\t]*<[Pp]([^A-Za-z0-9>][^>]*)?> *", "\n\n", $data);
$data = ereg_replace("[ \n\r\t]*<[Dd][Ii][Vv]([^A-Za-z0-9>][^>]*)?> *", "\n\n", $data);
$data = ereg_replace("\n\n\n+","\n\n", $data);
$data = ereg_replace("<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> *", "\n", $data);
$data = ereg_replace("<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> *", "\n", $data);
$data = ereg_replace("</[Tt][Dd]([^A-Za-z0-9>][^>]*)?> *", " ", $data);
$data = preg_replace("/\s*<[Hh][1-6]([^A-Za-z0-9>][^>]*)?> */", "\n\n", $data);
$data = preg_replace("/\s*<[Pp]([^A-Za-z0-9>][^>]*)?> */", "\n\n", $data);
$data = preg_replace("/\s*<[Dd][Ii][Vv]([^A-Za-z0-9>][^>]*)?> */", "\n\n", $data);
$data = preg_replace("/\n\n\n+/", "\n\n", $data);
$data = preg_replace("/<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data);
$data = preg_replace("/<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data);
$data = preg_replace("/</[Tt][Dd]([^A-Za-z0-9>][^>]*)?> */", " ", $data);
$data = preg_replace('/<\/p>/i', "\n\n", $data );
// Replace HTML entities
//$data = preg_replace("/&#([0-9]+);/e", 'chr(\1)', $data);
@ -327,7 +326,7 @@ class Convert {
}
return trim(wordwrap(trim($data), $wordWrap));
}
/**
* There are no real specifications on correctly encoding mailto-links,
* but this seems to be compatible with most of the user-agents.
@ -358,4 +357,4 @@ class Convert {
$f = Object::create('URLSegmentFilter');
return $f->filter($title);
}
}
}

View File

@ -257,7 +257,7 @@ class SapphireTestReporter implements PHPUnit_Framework_TestListener {
*/
private function getTestException(PHPUnit_Framework_Test $test, Exception $e) {
// get the name of the testFile from the test
$testName = ereg_replace('(.*)\((.*[^)])\)', '\\2', $test->toString());
$testName = preg_replace('/(.*)\((.*[^)])\)/', '\\2', $test->toString());
$trace = $e->getTrace();
// loop through the exception trace to find the original exception
for($i = 0; $i < count($trace); $i++) {

View File

@ -360,9 +360,13 @@ class Email extends ViewableData {
* @desc Validates the email address. Returns true of false
*/
static function validEmailAddress($address) {
return ereg('^([a-zA-Z0-9_+\.\-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$', $address);
if (function_exists('filter_var')) {
return filter_var($address, FILTER_VALIDATE_EMAIL);
} else {
return preg_match('#^([a-zA-Z0-9_+\.\-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$#', $address);
}
}
/**
* Send the email in plaintext.
*
@ -658,7 +662,7 @@ class Email_BounceHandler extends Controller {
}
private function recordBounce( $email, $date = null, $time = null, $error = null ) {
if(ereg('<(.*)>', $email, $parts)) $email = $parts[1];
if(preg_match('/<(.*)>/', $email, $parts)) $email = $parts[1];
$SQL_email = Convert::raw2sql($email);
$SQL_bounceTime = Convert::raw2sql("$date $time");

View File

@ -134,8 +134,8 @@ function htmlEmail($to, $from, $subject, $htmlContent, $attachedFiles = false, $
}
// Strip the human name from the bounce address
if(ereg('^([^<>]*)<([^<>]+)> *$', $bounceAddress, $parts)) $bounceAddress = $parts[2];
if(preg_match('/^([^<>]*)<([^<>]+)> *$/', $bounceAddress, $parts)) $bounceAddress = $parts[2];
// $headers["Sender"] = $from;
$headers["X-Mailer"] = X_MAILER;
if (!isset($customheaders["X-Priority"])) $headers["X-Priority"] = 3;
@ -215,7 +215,7 @@ function plaintextEmail($to, $from, $subject, $plainContent, $attachedFiles, $cu
if(isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) {
$bounceAddress = BOUNCE_EMAIL;
// Get the human name from the from address, if there is one
if(ereg('^([^<>]+)<([^<>])> *$', $from, $parts))
if(preg_match('/^([^<>]+)<([^<>])> *$/', $from, $parts))
$bounceAddress = "$parts[1]<$bounceAddress>";
} else {
$bounceAddress = $from;
@ -249,8 +249,7 @@ function plaintextEmail($to, $from, $subject, $plainContent, $attachedFiles, $cu
function encodeMultipart($parts, $contentType, $headers = false) {
$separator = "----=_NextPart_" . ereg_replace('[^0-9]','',rand() * 10000000000);
$separator = "----=_NextPart_" . preg_replace('/[^0-9]/', '', rand() * 10000000000);
$headers["MIME-Version"] = "1.0";
$headers["Content-Type"] = "$contentType; boundary=\"$separator\"";

View File

@ -144,7 +144,7 @@ class File extends DataObject {
*/
static function find($filename) {
// Get the base file if $filename points to a resampled file
$filename = ereg_replace('_resampled/[^-]+-','',$filename);
$filename = preg_replace('/_resampled/[^-]+-/', '', $filename);
// Split to folders and the actual filename, and traverse the structure.
$parts = explode("/", $filename);
@ -483,7 +483,7 @@ class File extends DataObject {
}
// Update title
if(!$this->getField('Title')) $this->__set('Title', str_replace(array('-','_'),' ',ereg_replace('\.[^.]+$','',$name)));
if(!$this->getField('Title')) $this->__set('Title', str_replace(array('-','_'),' ', preg_replace('/\.[^.]+$/', '', $name)));
// Update actual field value
$this->setField('Name', $name);

View File

@ -261,13 +261,13 @@ class Folder extends File {
$oldFile = $file;
if(strpos($file, '.') !== false) {
$file = ereg_replace('[0-9]*(\.[^.]+$)', $i . '\\1', $file);
$file = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $file);
} elseif(strpos($file, '_') !== false) {
$file = ereg_replace('_([^_]+$)', '_' . $i, $file);
$file = preg_replace('/_([^_]+$)/', '_' . $i, $file);
} else {
$file .= "_$i";
$file .= '_'.$i;
}
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file$ext with $i", E_USER_ERROR);
}

View File

@ -144,13 +144,13 @@ class Upload extends Controller {
// make sure archives retain valid extensions
if(substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.gz')) == '.tar.gz' ||
substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.bz2')) == '.tar.bz2') {
$relativeFilePath = ereg_replace('[0-9]*(\.tar\.[^.]+$)',$i . '\\1', $relativeFilePath);
$relativeFilePath = preg_replace('/[0-9]*(\.tar\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '.') !== false) {
$relativeFilePath = ereg_replace('[0-9]*(\.[^.]+$)',$i . '\\1', $relativeFilePath);
$relativeFilePath = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '_') !== false) {
$relativeFilePath = ereg_replace('_([^_]+$)', '_'.$i, $relativeFilePath);
$relativeFilePath = preg_replace('/_([^_]+$)/', '_'.$i, $relativeFilePath);
} else {
$relativeFilePath .= "_$i";
$relativeFilePath .= '_'.$i;
}
if($oldFilePath == $relativeFilePath && $i > 2) user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
}
@ -525,4 +525,4 @@ class Upload_Validator {
return true;
}
}
}

View File

@ -86,7 +86,7 @@ JS;
$i=0;
if($this->value) foreach($this->value as $part){
if(!$part || !(strlen($part) == 4) || !ereg("([0-9]{4})",$part)){
if(!$part || !(strlen($part) == 4) || !preg_match("/([0-9]{4})/", $part)){
switch($i){
case 0: $number = _t('CreditCardField.FIRST', 'first'); break;
case 1: $number = _t('CreditCardField.SECOND', 'second'); break;

View File

@ -79,12 +79,12 @@ class FieldGroup extends CompositeField {
if($subfield->getName()) $count++;
}
if($count == 1) $compositeTitle .= 'Group';
return ereg_replace("[^a-zA-Z0-9]+","",$compositeTitle);
return preg_replace("/[^a-zA-Z0-9]+/", "", $compositeTitle);
}
return ereg_replace("[^a-zA-Z0-9]+","",$this->title);
return preg_replace("/[^a-zA-Z0-9]+/", "", $this->title);
}
/**
* Returns a set of <span class="subfield"> tags, each containing a sub-field.
* You can also use <% control FieldSet %>, if you'd like more control over the generated HTML

View File

@ -123,7 +123,7 @@ class FormField extends RequestHandler {
* that this ID is included in the field.
*/
function ID() {
$name = ereg_replace('(^-)|(-$)','',ereg_replace('[^A-Za-z0-9_-]+','-',$this->name));
$name = preg_replace('/(^-)|(-$)/', '', preg_replace('/[^A-Za-z0-9_-]+/', '-', $this->name));
if($this->form) return $this->form->FormName() . '_' . $name;
else return $name;
}
@ -648,7 +648,7 @@ class FormField extends RequestHandler {
* @return string
*/
function Type() {
return strtolower(ereg_replace('Field$', '', $this->class));
return strtolower(preg_replace('/Field$/', '', $this->class));
}
/**
@ -769,4 +769,4 @@ class FormField extends RequestHandler {
else user_error("rootFieldSet() called on $this->class object without a containerFieldSet", E_USER_ERROR);
}
}
}

View File

@ -425,7 +425,7 @@ class HtmlEditorField_Toolbar extends RequestHandler {
$file = null;
} else {
$url = Director::makeRelative($request->getVar('FileURL'));
$url = ereg_replace('_resampled/[^-]+-','',$url);
$url = preg_replace('/_resampled/[^-]+-/', '', $url);
$file = DataList::create('File')->filter('Filename', $url)->first();
if(!$file) $file = new File(array('Title' => basename($url)));
}
@ -663,4 +663,4 @@ class HtmlEditorField_Image extends HtmlEditorField_File {
return ($this->file) ? $this->file->CMSThumbnail() : sprintf('<img src="%s" />', $this->url);
}
}
}

View File

@ -191,15 +191,15 @@ class i18nTextCollector extends Object {
public function collectFromCode($content, $module) {
$entitiesArr = array();
$regexRule = '_t[[:space:]]*\(' .
$regexRule = '#_t[[:space:]]*\(' .
'[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity
'[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value
'([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations
'([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
'\)';
while (ereg($regexRule, $content, $regs)) {
'\)#';
while (preg_match($regexRule, $content, $regs)) {
$entitiesArr = array_merge($entitiesArr, (array)$this->entitySpecFromRegexMatches($regs));
// remove parsed content to continue while() loop
@ -229,14 +229,15 @@ class i18nTextCollector extends Object {
}
// @todo respect template tags (< % _t() % > instead of _t())
$regexRule = '_t[[:space:]]*\(' .
$regexRule = '#_t[[:space:]]*\(' .
'[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity
'[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value
'([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations
'([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
'\)';
while(ereg($regexRule,$content,$regs)) {
'\)#';
while (preg_match($regexRule,$content,$regs)) {
$entitiesArr = array_merge($entitiesArr,(array)$this->entitySpecFromRegexMatches($regs, $fileName));
// remove parsed content to continue while() loop
$content = str_replace($regs[0],"",$content);
@ -301,7 +302,7 @@ class i18nTextCollector extends Object {
// remove wrapping quotes
$value = ($regs[2]) ? substr($regs[2],1,-1) : null;
$value = ereg_replace("([^\\])['\"][[:space:]]*\.[[:space:]]*['\"]",'\\1',$value);
$value = preg_replace("/([^\\])['\"][[:space:]]*\.[[:space:]]*['\"]/", '\\1', $value);
// only escape quotes when wrapped in double quotes, to make them safe for insertion
// into single-quoted PHP code. If they're wrapped in single quotes, the string should

View File

@ -395,8 +395,9 @@ abstract class SS_Database {
}
//Indexes specified as arrays cannot be checked with this line: (it flattens out the array)
if(!is_array($spec))
$spec = ereg_replace(" *, *",",",$spec);
if(!is_array($spec)) {
$spec = preg_replace('/\s*,\s*/', ',', $spec);
}
if(!isset($this->tableList[strtolower($table)])) $newTable = true;
@ -470,7 +471,7 @@ abstract class SS_Database {
// collations.
// TODO: move this to the MySQLDatabase file, or drop it altogether?
if(!$this->supportsCollations()) {
$spec = eregi_replace(' *character set [^ ]+( collate [^ ]+)?( |$)','\\2',$spec);
$spec = preg_replace('/ *character set [^ ]+( collate [^ ]+)?( |$)/', '\\2', $spec);
}
if(!isset($this->tableList[strtolower($table)])) $newTable = true;
@ -632,8 +633,8 @@ abstract class SS_Database {
* @param string $array Array where the replacement should happen
*/
static function replace_with_null(&$array) {
$array = ereg_replace('= *\'\'', "= null", $array);
$array = preg_replace('/= *\'\'/', '= null', $array);
if(is_array($array)) {
foreach($array as $key => $value) {
if(is_array($value)) {

View File

@ -172,7 +172,7 @@ class Image extends File {
while(file_exists(BASE_PATH . "/$file")) {
$i = $i ? ($i+1) : 2;
$oldFile = $file;
$file = ereg_replace('[0-9]*(\.[^.]+$)',$i . '\\1', $file);
$file = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $file);
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file with $i", E_USER_ERROR);
}

View File

@ -30,10 +30,10 @@ class Date extends DBField {
}
// Default to NZ date format - strtotime expects a US date
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
if(preg_match('#^([0-9]+)/([0-9]+)/([0-9]+)$#', $value, $parts)) {
$value = "$parts[2]/$parts[1]/$parts[3]";
}
if(is_numeric($value)) {
$this->value = date('Y-m-d', $value);
} elseif(is_string($value)) {
@ -357,4 +357,4 @@ class Date extends DBField {
public function scaffoldFormField($title = null, $params = null) {
return new DateField($this->name, $title);
}
}
}

View File

@ -27,10 +27,10 @@ class SS_Datetime extends Date {
function setValue($value) {
// Default to NZ date format - strtotime expects a US date
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
if(preg_match('#^([0-9]+)/([0-9]+)/([0-9]+)$#', $value, $parts)) {
$value = "$parts[2]/$parts[1]/$parts[3]";
}
if(is_numeric($value)) {
$this->value = date('Y-m-d H:i:s', $value);
} elseif(is_string($value)) {

View File

@ -63,7 +63,7 @@ class Varchar extends StringField {
* Ensure that the given value is an absolute URL.
*/
function URL() {
if(ereg('^[a-zA-Z]+://', $this->value)) return $this->value;
if(preg_match('#^[a-zA-Z]+://#', $this->value)) return $this->value;
else return "http://" . $this->value;
}

View File

@ -180,7 +180,7 @@ class PermissionCheckboxSetField extends FormField {
$odd = ($odd + 1) % 2;
$extraClass = $odd ? 'odd' : 'even';
$extraClass .= ' val' . str_replace(' ', '', $code);
$itemID = $this->id() . '_' . ereg_replace('[^a-zA-Z0-9]+', '', $code);
$itemID = $this->id() . '_' . preg_replace('/[^a-zA-Z0-9]+/', '', $code);
$checked = $disabled = $inheritMessage = '';
$checked = (isset($uninheritedCodes[$code]) || isset($inheritedCodes[$code])) ? ' checked="checked"' : '';
$title = $permission['help'] ? 'title="' . htmlentities($permission['help'], ENT_COMPAT, 'UTF-8') . '" ' : '';
@ -281,4 +281,4 @@ class PermissionCheckboxSetField_Readonly extends PermissionCheckboxSetField {
function saveInto($record) {
return false;
}
}
}