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})/"
Related
This question already has answers here:
Replace preg_replace() e modifier with preg_replace_callback
(3 answers)
Closed 2 years ago.
(This question is not a duplicate of this.)
How can I capitalize the string llc when found within another string, surrounded by word boundaries? In other words, I don't want to captilalize llc if it's in the middle of another word, such as ballcap, but only when it's by itself, either surrounded by spaces, end of string, etc. Such as my big company, llc.
I have the following line of PHP that worked fine in PHP 5.6, to capitalize the string llc whenever found within another string:
$foo = trim(preg_replace('/\bllc\b/ie', 'strtoupper("$0")', $str));
But in PHP 7.4 (and maybe all 7.x), that always returns empty string, regardless of what string is fed in for the value of $str.
I have tried using preg_replace_callback but can't seem to get the syntax right for my use case.
Example: big company, llc should become big company, LLC
Any idea how I can get this working in 7.4?
e modifier is deprecated, you should use preg_replace_callback
It runs a function with an array of all matches and uses return value as replacement
$foo = trim(preg_replace_callback('/\bllc\b/i', function($matches) {
return strtoupper($matches[0]);
}, $str));
This question already has answers here:
Simple preg_replace returns null
(3 answers)
Closed 8 years ago.
You'll have to forgive me as I am really bad at regex.
So here is what I'm trying to do. I'm working on pulling Option Chains from Google Finance's API. The only problem is Google doesn't wrap the JSON keys in quotes like they should be. I found a method of using a replace string method with a regex but it is in R and I'm working with PHP.
The JSON I'm working with is from here
R:
gsub('([^,{:]+):', '"\1":', json)
PHP:
$pattern = '([^,{:]+):';
$replacement = '"\1":';
$json = preg_replace($pattern, $replacement, $quote);
I tried the PHP code seen above and I'm getting:
[18-Jan-2015 21:34:36 America/Denver] PHP Warning: preg_replace(): Unknown modifier ':' in /home1/oldpizza/public_html/austingregory/stocks/index.php on line 10
I'm betting it is the difference between PHP and R regex patterns but I'm not sure about the difference as I am still very new to regex in general. But according to regex101 it should work...
Not quite sure where to go from there. If you could help me out with the regex or help me figure out why it isn't working that would be great.
Thank you!
Delimiter is required in preg_replace:
$pattern = '~([^,{:]+):~';
$replacement = '"\1":';
$json = preg_replace($pattern, $replacement, $quote);
/, ~ and # are usually used as delimiter, but () pair can also be used as delimiter, and that is the reason why you got the warning above. It is interpreting the outermost pair of () as delimiter, and : at the end as modifier (flag).
This question already has answers here:
Replace preg_replace() e modifier with preg_replace_callback
(3 answers)
Closed 2 years ago.
ErrorException [ 8192 ]: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead
on
http://mastersoftwaretechnologies.com/kohana/index.php/user/login
This works in my kohana-3.2
Replace
$column = preg_replace('/"(.+?)"/e', '$this->quote_column("$1")', $column);
to
$column = preg_replace_callback('/"(.+?)"/', function($m) { return $this->quote_column($m[1]); }, $column);
In the file MODPATH/database/classes/kohana/database.php line 525
Looks like you are using php 5.5. If you want to use preg replace eval with it you have to modify it firts because it is deprecated due to security reasons: http://php.net/manual/en/migration55.deprecated.php
With php 5.5 you should use preg_replace_callback()
Good example you will find here: Replace deprecated preg_replace /e with preg_replace_callback
And info how it works here: Replace preg_replace() e modifier with preg_replace_callback
Just modify second paremeter accordingly to that what you want to do with matches.
This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm trying to extract some text in between a Request XML tag (between and tags) using this Regex:
(?<=(<Request>)).*(?=(</Request>))
RegexBuddy shows me that it's fine, but preg_match returns this error:
"Unknown modifier R". Placing a backslash before the "/" makes it so nothing returns. Does anyone know what the problem is?
Code:
$parsedQuery = fopen("c:\\query", "r") ;
$parsed ="".
while (!feof($parsedQuery)) {
$parsed .= fgets($parsedQuery) ;}
$reg = "#(?<=(<Request>)).*(?=(</Request>))#";
$match = array();
preg_match($reg, $parsed, $match);
print_r($match);
Edit: I now noticed that the file opens with an unidentified character (binary value is 3F) after the opening of each tag (the "<" character). I assume php's fgets implementation does this for security measures, could this be the problem, and is there any way to surpass it?
The problem is that in PHP (and in a lot of langugages) a regexp is something like /pattern/modifier where modifier could be, for example, g (multiple match). If you want to use / in your regexp you have to escape them with a \ :
/(?<=(<Request>)).*(?=(<\/Request>))/
See http://www.php.net/manual/en/pcre.pattern.php for more information about patterns in 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.