Replace Ereg_Replace with Preg_Replace

replace ereg_replace with preg_replace

To port ereg_replace to preg_replace you need to put the regex between a pair of delimiter

Also your regx is [\] is invalid to be used for preg_replace as the \ is escaping the closing char class ]

The correct port is

preg_replace('/[\\\]/','',$theData) 

Also since the char class has just one char there is no real need of char class you can just say:

preg_replace('/\\\/','',$theData) 

Since you are replace just a single char, using regex for this is not recommended. You should be using a simple text replacement using str_replace as:

str_replace('\\','',$data);

What to change from ereg_replace to preg_replace?

Try this one:

preg_replace("/&page=[0-9]+/","",$_SERVER['QUERY_STRING']);

Hope this helps :)

quick ereg_replace to preg_replace

Well, this is not a regular expression that you are using. What you want here is str_replace.

$row[$j] = str_replace("\n", "\\n", $row[$j]); 

Rather read up about what regular expressions are and how they work.

Replacing ereg_replace()

The code you posted replaces the backslashes with nothing and it doesn't match the square brackets; they are used to create a character range that contains only the backslash character and are completely useless on your regex.

The equivalent preg_replace() statement is:

$row['subject'] = preg_replace('/\\\\/', '', $row['subject']);

The regex is /\\/. Using a single backslash (\) produces an error; the backslash is an escape character in regex, it escapes the / making it be interpreted literally and not as the delimiter. The two extra backslashes are needed because the backslash is also an escape character in PHP and it needs to be escaped too.

I guess this was the reason the original coder enclosed the backslash into a range, to circumvent the need for escaping and double escaping.

Using the same trick you can write it with preg_replace() as:

$row['subject'] = preg_replace('/[\]/', '', $row['subject']);

A single backslash is enough. In the regex the backslash escapes are not allowed in character ranges. And in PHP single-quoted strings, the backslash needs to be escaped only if it is the last character from the string.

(I know, the documentation teaches us to double all the backslashes, but, on the other hand, it says that a backslash that does not precede an apostrophe or another backslash in single-quoted strings is interpreted literally.)

Back to the regex, there is a better (faster, cleaner) way to rewrite the above call to ereg_replace(): do not use regex at all. Because all it does is to match (and replace) the backslashes, you don't need to use regex. A simple str_replace() is enough:

$row['subject'] = str_replace('\\', '', $row['subject']);

Conversion of ereg_replace to preg_replace, PHP 5,3

the given examples should work like this:

$line2 = preg_replace("/[\n\r]/", "", $line);

$line2 = preg_replace("/\t\t+/", "", $line2);

In some cases you need other modifications, too. For example if you used the / inside your regex then you need to escape it like this: \/

please read this thread: ereg_replace to preg_replace?

ereg_replace to preg_replace?

preg_replace("/.*\.(.*)$/", "\\1", "foo.jpg")

I don't know why PHP requires the / delimiters. The only reason Perl, JS, etc. have them is that they allow regex literals, which PHP doesn't.

error when changing ereg_replace to preg_replace

So your regexes are as follows:

"'\/\*.*\*\/'si"
"'<\?.*?\?>'si"
"'<%.*?%>'si"
"'<script[^>]*?>.*?</script>'si"

Taking those one at a time, you are first greedily stripping out multiline comments. This is almost certainly where your memory problem is coming from, you should ungreedify that quantifier.

Next up, you are stipping out anything that looks like a PHP tag. This is done with a lazy quantifier, so I don't see any issue with it. Same goes for the ASP tag, and finally the script tag.

Leaving aside potental XSS threats left out by your regex, the main issue seems to be coming from your first regex. Try "'\/\*.*?\*\/'si" instead.

convert ereg_replace to preg_replace in php

You will have a more efficient pattern with this:

preg_replace ('/.*alternative0="[^"]++"[^>]*+>/', "", $v, 1);

You can't use something like: .*> because the dot match all characters and the quantifier * is greedy thus the .* match the final > too and the > from the pattern is never matched or not matched at the good offset.



Related Topics



Leave a reply



Submit