Functions General

eregi

Posted 02 October 2010 – 02:34 PM
preg_match()  Would be the pcre equivalent.  The patterns will pretty much be the same, only real difference is that the preg_xxx functions require an opening/closing delimiter in the pattern and modifiers (if needed) are specified after the closing delimiter.
So for instance:
ereg(".",$var1)
would be
preg_match("~.~",$var1)  // ~ is used as the pattern delimiter.
or for instance,

eregi(".",$var1) // case in-sensitive
would be
preg_match("~.~i",$var1)  // i modifier added to make it case in-sensitive
if(eregi("color",$fieldName))
preg_match("~color~i",$fieldName))

eregi

http://docstore.mik.ua/orelly/webprog/pcook/ch13_02.htm
13.2. Switching From ereg to preg

13.2.1. Problem
   You want to convert from using ereg functions to preg functions.

13.2.2. Solution
First, you have to add delimiters to your patterns:
preg_match('/pattern/', 'string')
For eregi( )   case-insensitive matching, use the /i modifier instead:
preg_match('/pattern/i', 'string');
When using integers instead of strings as patterns or replacement values, convert the number to hexadecimal and specify it using an escape sequence:
$hex = dechex($number);
preg_match("/\x$hex/", 'string');

13.2.3. Discussion
There are a few major differences between ereg and preg. First, when you use preg  functions, the pattern isn't just the string pattern; it also needs delimiters, as in Perl, so it's /pattern/ instead.[11] So:

[11]Or {}, <>, ||, ##, or whatever your favorite delimiters are. PHP supports them all.
ereg('pattern', 'string');
becomes:
preg_match('/pattern/', 'string');
When choosing your pattern delimiters, don't put your delimiter character inside the regular-expression pattern, or you'll close the pattern early. If you can't find a way to avoid this problem, you need to escape any instances of your delimiters using the backslash. Instead of doing this by hand, call addcslashes( ).
For example, if you use / as your delimiter:
$ereg_pattern = '<b>.+</b>';
$preg_pattern = addcslashes($ereg_pattern, '/');
The value of $preg_pattern is now <b>.+<\/b>.
  The preg functions don't have a parallel series of case-insensitive functions. They have a case-insensitive modifier instead. To convert, change:
eregi('pattern', 'string');
to:
preg_match('/pattern/i', 'string');
Adding the i after the closing delimiter makes the change.
Finally, there is one last obscure difference. If you use a number (not a string) as a pattern or replacement value in ereg_replace( ) , it's assumed you are referring to the  ASCII value of a character. Therefore, since 9 is the ASCII representation of tab (i.e., \t), this code inserts tabs at the beginning of each line:
$tab = 9;
$replaced = ereg_replace('^', $tab, $string);
Here's how to convert linefeed endings:
$converted = ereg_replace(10, 12, $text);
To avoid this feature in ereg functions, use this instead:
$tab = '9';
On the other hand, preg_replace( ) treats the number 9 as the number 9, not as a tab substitute. To convert these character codes for use in preg_replace( ), convert them to hexadecimal and prefix them with \x. For example, 9 becomes \x9 or \x09, and 12 becomes \x0c. Alternatively, you can use \t  , \r, and \n for tabs, carriage returns, and linefeeds, respectively.

This is line 466:
if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
I tried simply replacing with preg_match, but it couldn't recognize the = modifier in the regular expression.. I'm not too good with regular expression yet and solving this requires that I learn the regexp ereg needs AND the regexp preg_match needs (which, if I am not mistaken, is different)… Could you guys help me out with this one?
Thanks
php regex pcre posix-ere

share|improve this question

edited Aug 5 '10 at 10:26

Gumbo
292k42412549

asked Feb 7 '10 at 18:51

Shawn
1,60852455

  
add a comment

3 Answers  

activeoldestvotes

up vote6down voteaccepted

POSIX extended regular expressions (POSIX ERE, used by ereg) and Perl-combatible regular expressions (PCRE, used by preg_match) are very similar. Except from some special POSIX expressions, PCRE is a superset of POSIX ERE.
That means you just need to put your POSIX ERE regular expressions into delimiters (here /) and escape any occurrence of that character inside the regular expression and you have a valid PCRE regular expression:
/^([^=]*)=["']?([^"']*)["']?$/
So:
preg_match('/^([^=]*)=["\']?([^"\']*)["\']?$/', $v, $a3)

share|improve this answer

=================================

if (eregi("[<>]",$reply))$found=true;

$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<A HREF='#' onclick=\"LoadPopup('\\0',800,800);\">\\0</a>", $c);
    $text = ereg_replace("[[:alpha:]]+@[^<>[:space:]]+[[:alnum:]/]","<A HREF='mailto:\\0'>\\0</a>", $text);

Find String

$l=strpos($string,$character,$offset);
InStr($offset, $string,$charcter) compare vb.net

Length

$l=strlen($string);

Html entities

$history = htmlentities(mb_convert_encoding($history, ‘UTF-8’, ‘ASCII’), ENT_SUBSTITUTE, “UTF-8”);
Convert your html code to appear in textarea properly.

Html Tags

<?php
$text = '<p>Test paragraph.</p><!– Comment –> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>

Lower Case

$stringnew=strtolower($string);
[First Caps]
$string = strtolower($string);
$string = substr_replace($string, strtoupper(substr($string, 0, 1)), 0, 1);

Pad

Run example »
——————————————————————————–
Definition and Usage
The str_pad() function pads a string to a new length.
——————————————————————————–
Syntax

str_pad(string,length,pad_string,pad_type)

Parameter
Description
string Required. Specifies the string to pad
length Required. Specifies the new string length. If this value is less than the original length of the string, nothing will be done
pad_string Optional. Specifies the string to use for padding. Default is whitespace
pad_type Optional. Specifies what side to pad the string.
Possible values:
•STR_PAD_BOTH – Pad to both sides of the string. If not an even number, the right side gets the extra padding
•STR_PAD_LEFT – Pad to the left side of the string
•STR_PAD_RIGHT – Pad to the right side of the string. This is default

Technical Details

Section of String

$newstring=substr($string,int start, int length);
$url=$row['url'];
if($url!=""){
    if(substr($url,0,4)=="www.")$url="http://".$url;
    if(substr($url,0,7)!="http://")$url="http://".$url;
}

String Replace

$longadd=str_replace(chr(13),’
‘,$longadd);