InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Switching from ereg to preg in PHP
PHP

Switching from ereg to preg in PHP

InfinityCoder December 24, 2016

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

First, you have to add delimiters to your patterns:

1
preg_match('/pattern/', 'string');

For case-insensitive matching, use the /i modifier with preg_match() instead:

1
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:

1
2
$hex = dechex($number);
preg_match("/\x$hex/", 'string');

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.1 So:

1
ereg('pattern', 'string');

becomes:

1
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:

1
2
$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:

1
eregi('pattern', 'string');

to:

1
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, because 9 is the ASCII representation of tab (i.e., \t), this code inserts tabs at the beginning of each line:

1
2
$tab = 9;
$replaced = ereg_replace('^', $tab, $string);

Here’s how to convert linefeed endings:

1
$converted = ereg_replace(10, 12, $text);

To avoid this feature in ereg functions, use this instead:

1
$tab = '9';

On the other hand, preg_replace() treats the number 9 as the one-character string ‘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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Program: whereis in PHP
Although tools such as phpDocumentor provide quite detailed information about …

Program: whereis in PHP

Assigning Object References in PHP
You want to link two objects, so when you update …

Assigning Object References in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap