replace ereg_replace with preg_replace [duplicate] - php

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

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.

What is the regular expression I should use in preg_replace() to find tabs inside all quoted strings and replace with a single space? [duplicate]

This question already has answers here:
How do I replace tabs with spaces within variables in PHP?
(9 answers)
Replace all occurences of char inside quotes on PHP
(5 answers)
Closed 5 years ago.
I know what needs to be done, but have been unsuccessful with the correct regex.
What is the regular expression I should use in preg_replace() to find tabs inside all quoted strings and replace with a single space?
any help would be much appreciated.
Example:
$string = '"Foo\tMan\tChoo"'
preg_replace($expression_string, ' ',$string);
echo $string;//desired result---->'"Foo Man Choo"'
I think this question is not a duplicate
answer : you will need to repeat preg_replace as many time as the max of \t you expect in a field (never could be bothered comming up with a more generic solution : it is usually possible to use this one)
also make sure every line ends with a tab (otherwise the last field will not be processed)
then you need to repeat replacement starting with the max possible number of \t (2 in the example)
$string = '"Foo'."\t".'Man'."\t".'Choo"'."\t".'boo'."\t".'"Foo'."\t".'Man"'."\t";
$string = preg_replace('/"([^"]*)'."\t".'([^"]*)'."\t".'([^"]*)"'."\t".'/','"$1_$2_$3"'."\t",$string);
$string = preg_replace('/"([^"]*)'."\t".'([^"]*)"'."\t".'/','"$1_$2"'."\t",$string);
echo $string;

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.

Preg_replace with multiple lines(php) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace text over multiple lines using preg_replace
I am trying to read some text from a file and than replacing some pattern. If I try to replace the patern from just a single string it works, but if there are multiple such strings in a file it doesnt work.
$this->session->set_flashdata('error_message', 'Naslovna vrstica je bila uspešno shranjena');
This is an example of text that I am trying to replace the replacment works ok with just this line, but not if there are other such lines in a file, which all match individualy though.
$content = file_get_contents("C:\Users\Borut\\test.txt");
$pattern="/^.*session->set_flashdata\((.*),(.*)\);$/";
$replacement="\$_SESSION[$1]=$2";
this is my code. How do you replace multiple strings as the one shown above.
The modifier you want is m. You can find all modifiers here
That said, the easiest and better regex solution would be
"/\$this->session->set_flashdata\((.*?),\s*(.*?)\);/"
Notice how there's a ? after the .* in each. This is to stop greedy matching as with yours. Also notice that the modifier isn't required either with the removal of the ^ and $

replacement for ereg_replace [duplicate]

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"]
);

Categories