replacement for ereg_replace [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
I have upgraded php and now im getting the ereg_replace deprecated errors.
I have done some searching round web and found that I can use preg instead but not sure how to change this code correctly
$scriptName = ereg_replace(
"^".$_SERVER["DOCUMENT_ROOT"], "",
$_SERVER["SCRIPT_FILENAME"]
);

Replace the e with a p.
Add a delimiter to the beginning and end of that first argument. Traditionally, people use slashes (/), but I like to use ~ as there is less chance of actually using that character in the regular expression.

Just adding delimiters won't work when special characters are included in $_SERVER["DOCUMENT_ROOT"]'s value. You need to escape them as follows:
$scriptName = preg_replace(
"/^".preg_quote($_SERVER["DOCUMENT_ROOT"],"/")."/",
"",
$_SERVER["SCRIPT_FILENAME"]
);

Related

What is the use of "ereg_replace("\n","\\n",$row[$j])" expression? [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 2 years ago.
I found one PHP script for get a database backup, there use this expression to do something, that script show call to undefined function ereg_replace() this error, if i remove this line script is working fine...
how to replace this function $row[$j] = ereg_replace("\n","\\n",$row[$j]); to that script working finely,
Can anyone assist me..
ereg_replace deprecated and remove from newer versions of php (7 and up).
You will have to update your code to use preg_replace
$row[$j] = preg_replace("#\n#","\\n",$row[$j]);
One of the differences between ereg_replace() and preg_replace() is that the pattern must be enclosed by delimiters: delimiter + pattern + delimiter, in this case we are using # so we don't have to escape / that is the usual that is used.
Valid Delimiters:
/, #, ~, +, %, #, ! , <, >
Reference from php.net
hope it helps.

php - valid regEx expression in ereg_replace makes nothing [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 8 years ago.
I'm using PHP 5.2.17. I want to remove some surplus data from a JSON string and I've thought I can use some replace function to do so. Specifically I'm using ereg_replace with the next expression:
'^.*?(?=\"created_at)'
Which I've validated at http://www.regexpal.com. I've pasted my JSON string in there and the match is right. However, when I make the call:
$tweets = eregi_replace('^.*?(?=\"created_at)', $temp, 'something');
and then I echo the $tweets variable, there's output. No errors in console neither. Apache error log, however, complains about an error called REG_BADRPT. There's a comment in the php docs of eregi_replace suggesting this can be due to I need to escape special characters, but I've already escaped the " character. And I've tried to escape others but no different behavior.
Where could the problem be then?
I don't think that ereg supports lookarounds. preg_replace exists in php 5.2, so you should really use that instead. It will work with your expression with delimiters.
$tweets = preg_replace('#^.*?(?=\"created_at)#i', 'something', $temp);
As other people have pointed out, ereg functions are deprecated, so use preg_replace. You also have to encapsulate your regex string in slashes (/). You can put your regex flags after your last slash.

Is this regex right (eregi to preg_match conversion) [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I am replacing PHP deprecated functions in my website. I have this code:
(eregi("<[^>]*object.*\"?[^>]*>", $secvalue))
It is written on the php.net site that it eregi should be replaced with preg_match with i modifier.
Is this coded right?
(preg_match("<[^>]*object.*\"?[^>]*/i>", $secvalue))
or should I place /i somewhere else?
preg_match('/<[^>]*object.*\"?[^>]*>/i', $secvalue)
You need to add a forward slash at the beginning to match the closing one:
preg_match('/<[^>]object."?[^>]*>/i', $secvalue);

eregi > preg_match convert [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I need this php code changed to preg_match please
if(eregi("someurl",$data))
if (preg_match('~someurl~i', $data)). Delimiters can be any single character — I've used ~ instead of / used in examples on PHP site, because if you're putting URL in there, you'd have to escape /. i after the second delimiter is a flag triggering case-insensitivity — PCRE doesn't have separate functions for that.

replace ereg_replace with preg_replace [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
Hi need to change the function ereg_replace("[\]", "", $theData) to 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);
str_replace("\\","",$theData);
But I seriously doubt you need that replace at all. most likely you need some other operation.
What is this replace for?
preg_replace("/\\\/", "", $theData);
I used this sed to automatically replace ereg_replace by preg_replace and put the required slashes in. Assumes no \" in the first regex
sed -i 's#ereg_replace("\([^"]*\)"#preg_replace("/\1/"#g' *.php

Categories