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

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.

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.

Change preg_replace into preg_replace_callback [duplicate]

This question already has answers here:
Replace preg_replace() e modifier with preg_replace_callback
(3 answers)
Closed 7 years ago.
I'm trying to replace the following string:
\u0080\u009cone hand rule\u0080\u009d
As I have to replace those codes for many strings and as there are various combinations of \u0080\u009 like \u00e2\u0080\u0099 and \u00a0 I ve searched for an solution to convert them all back into their according value.
The solution i ve found was posted on this site: http://www.avoid.org/replace-u-characters-in-json-string/
preg_replace("/\\\\u([a-f0-9]{4})/e",
"iconv('UCS-4LE','UTF-8', pack('V', hexdec('U$1')))",
json_encode($struct));
As i was trying to use this code php threw the following error:
Deprecated: preg_replace(): The /e modifier is deprecated, use
preg_replace_callback instead
Since I'm relativley new to php i bareley understand the preg_replace in first place but with the conversion to preg_replace_callback i'm totally overstrained. I've looked up How to convert preg_replace e to preg_replace_callback? but i didnt get it :/
Could somone pls explain how to convert my preg_replace into preg_replace_callback?
The code I m trying to get to work looks now like this:
$string = preg_replace("/\\\\u([a-f0-9]{4})",
function($match){
return = iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')), $match[1]);
},
json_encode($string));
I ve read and understood how to access my found matches but since the
"iconv('UCS-4LE','UTF-8', pack('V', hexdec('U$1')))",
from the original code doesnt access any of the matches found I'm quiet confused how to set up my anonymous function
As of PHP 5.5.0 E_DEPRECATED level error is emitted when passing in the "\e" modifier. As of PHP 7.0.0 E_WARNING is emited in this case and "\e" modifier has no effect.
Please visit preg_replace and preg_replace_callback for documentation
"/\\u([a-f0-9]{4})" = > "/\\u([a-f0-9]{4})/"

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

What does the Unknown modifier 'c' mean in Regex? [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm a newbie with regular expressions and i need some help :).
I have this:
$url = '<img src="http://mi.url.com/iconos/oks/milan.gif" alt="Milan">';
$pattern = '/<img src="http:\/\/mi.url.com/iconos/oks/(.*)" alt="(.*)"\>/i';
preg_match_all($pattern, $url, $matches);
print_r($matches);
And I get this error:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'c'
I want to select that 'milan.gif'.
How can I do that?
If you’re using / as delimiter, you need to escape every occurrence of that character inside the regular expression. You didn’t:
/<img src="http:\/\/mi.url.com/iconos/oks/(.*)" alt="(.*)"\>/i
^
Here the marked / is treated as end delimiter of the regular expression and everything after is is treated as modifier. i is a valid modifier but c isn’t (see your error message).
So:
/<img src="http:\/\/mi\.url\.com\/iconos\/oks\/(.*)" alt="(.*)"\>/i
But as Pekka already noted in the comments, you shouldn’t try to use regular expressions on a non-regular language like HTML. Use an HTML parser instead. Take a look at Best methods to parse HTML.
The problem is that you haven't escaped the forward slashes in the url string (you have escaped the ones in the http:// part, but not the url path).
Therefore the first one it comes across it (which is after .com), it thinks is the end of the regex, so it treats everything after that slash as the 'modifier' codes.
The next character ('i') is a valid modifier (as you know, since you're actually using it in your example), so that passes the test. However the next character ('c') is not, so it throws an error, which is what you're seeing.
To fix it, simply escape the slashes. So your example would look like this:
$pattern = '/<img src="http:\/\/mi.url.com\/iconos\/oks\/(.*)" alt="(.*)"\\>/i';
Hope that helps.
Note, as someone has already said, it's generally not advisable to use regex to match HTML, since HTML can be too complex to match accurately. It's generally preferrable to use a DOM parser. In your example, the regex could fail if the alt attribute or the end of the image URL contains unexpected characters, or if the quoting in the HTML code isn't as you expect.

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