PHP » PYTHON |
login |
register |
about
|
PYTHON ereg_replace
is this article helpful?
|
Python replacement for PHP's ereg_replace
[
edit
| history
]
You need to import the Regular Expression module re and use the sub method.
import re re.sub(regex, replacement, subject) Performs a search-and-replace across subject, replacing all matches of regex in subject with replacement. The result is returned by the sub() function. The subject string you pass is not modified. http://www.regular-expressions.info/python.html ereg_replace(PHP 4, PHP 5) ereg_replace — Replace regular expression Description
string ereg_replace
( string $pattern
, string $replacement
, string $string
)
This function scans string for matches to pattern , then replaces the matched text with replacement . Parameters
Return ValuesThe modified string is returned. If no matches are found in string , then it will be returned unchanged. ExamplesFor example, the following code snippet prints "This was a test" three times:
Example #1 ereg_replace() example
<?php
One thing to take note of is that if you use an integer value as the replacement parameter, you may not get the results you expect. This is because ereg_replace() will interpret the number as the ordinal value of a character, and apply that. For instance:
Example #2 ereg_replace() example
<?php
Example #3 Replace URLs with links
<?php
NotesTip
preg_replace(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace(). See Also
|
more
Recently updated
more
Most requested
more
Last requests
|