Convert deprecated eregi/ereg/ereg_replace to preg

2019-01-29 07:47发布

Today I publish a site on a new domain and new hosting provider but get depreciated warnings at some lines in the code. I'm not good at preg stuff but maybe can somebody help me to convert it to equivalent preg_match code?

Here some lines of code:

/* 1 */
$b = ( eregi( "^https?://(.*).$sDomainName/", $q ) ||  eregi( "^https?://$sDomainName/", $q ));

/* 2 */
function suIsValidEmail( $s ) 
{ return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $s); }

/* 3 */
if( !eregi( '^https?://*/', $aa['src'] ))

/* 4 */
 $sText = ereg_replace('[^A-Za-z0-9 &;'.suMakeString( $asInclNonNumChars ).']', ' ', strip_tags( $s ));

/* 5 */
function suPlainTextLinksToHtml( &$s )
{ // convert all links to html links 
  $s = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $s ); 
}

/* 6 */
function suPlainTextEmailToHtml( &$s )
{    
  // Convert all email to links:
  $s = ereg_replace('[-a-z0-9!#$%&.\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*', '<a href="mailto:\\0">\\0</a>', $s );
}

/* 7 */
if( $s === $sReferer || eregi( "^https?://$s/", $sReferer ) )
 { return true; }

/* 8 */
function suWildCardToRegExpression( $str )
{
  $s = ''; 
  for ($i = 0; $i < strlen($str); $i++)
  {
   $c = $str{$i};
   if ($c =='?')
   $s .= '.'; // any character
   else if ($c == '*')   
   $s .= '.*'; // 0 || more any characters   
   else if ($c == '[' || $c == ']')
   $s .= $c;  // one of characters within []
   else
   $s .= '\\' . $c;
  }

  $s = '^' . $s . '$';

  //trim redundant ^ || $
  //eg ^.*\.txt$ matches exactly the same as \.txt$
  if (substr($s,0,3) == '^.*')
   $s = substr($s,3);
  if (substr($s,-3,3) == '.*$')
   $s = substr($s,0,-3);
  return $s;
}

function suIsFileNameMatch( $asFileMask, $sFileName )
{
  if( !is_array( $asFileMask ))
  { if( is_string( $asFileMask ))
      { $asFileMask = explode( ';', $asFileMask ); }
     else { $asFileMask = (array)$asFileMask; }
  } 

  if( suIsValidArray( $asFileMask ))
  {
    foreach( $asFileMask as $sFileMask )
    {
      $bResult = ereg( suWildCardToRegExpression( (string)$sFileMask ), $sFileName );
      if( $bResult )
       { return true; }
    }
  }

  return false;
}   

NOTE: I am not a lazy person but I simply don't know how to do it!

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-29 08:35

A small guide:

  • In preg, you need to surround the expression with a delimiter character; a standard one is /, but it can be others too (especially if you match URLs) such as @ or ~. For example preg_match('#^https?://#i') is the same as eregi('^https?://').

  • eregi can be converted by appending a pattern modifier i, like the above example.

  • ereg_replace translates to preg_replace and so does eregi_replace (but remember to add the i modifier).

  • escaping a variable inside an expression can be done using preg_quote.

Not sure if that covers all, but I welcome others to chime in.

See also: http://docstore.mik.ua/orelly/webprog/pcook/ch13_02.htm

查看更多
登录 后发表回答