Moving from preg_replace to preg_replace_callback [duplicate] - php

This question already has answers here:
Replace preg_replace() to preg_replace_callback() [duplicate]
(2 answers)
Closed 5 years ago.
I've had a good look through all the previous topics and I don't understand enough PHP to use them to answer my questions so sorry in advance if this is really simple!
{
$content = preg_replace('/\$([\w]+)/e', '$0', $this->getTemplateStyle());
$custom_css = $this->getCustomCSS();
return $content.$custom_css;
}
And I need to replace preg_replace with preg_replace_callback. I know it's not a simple switch and that I need to add more to the code, but I don't know what to add. Thanks in advance for your help.

[SOLVED]
You seem to be trying to inject variables from the current scope into your string. I'll leave aside why this is a bad idea and assume there is no user input involved. First get the current scope:
$scope = get_defined_vars();
Next use the callback:
preg_replace_callback('/\$(\w+)/',function($m) use ($scope) {if( isset($scope[$m[1]])) return $scope[$m[1]]; else return $m[0];}, $this->getTemplateStyle());
Job done. – #Niet the Dark Absol

Related

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

"Function eregi() is deprecated" tried preg_match - no bueno [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 4 years ago.
I've read many threads here about this being deprecated and tried replacing it with preg_match but I don't know php enough to fix the rest of the line.
Been enjoying Fotoholder for years, I would switch to a newer similar single file gallery code but then I would lose all my descriptions in the gallery.
Please help resurrect Fotopholder!
( https://github.com/offsky/Fotopholder )
Here are the 2 parts that have eregi:
if(substr($entry,0,1)!="." && !preg_match("#_cache#i",$entry) && is_dir($path."/".$entry)) {
and the 2nd eregi:
if(substr($entry,0,1)!="." && !eregi("_cache",$entry)) {
Thank you very much for your help.
Deprecated means, that the function eregi() is likely to get deleted from the language.
Please use preg_match().
While you still can use eregi(), at a certain point of time your application might not be able to execute any more.
That said: Your code posted is far to big to get a detailed answer.

PHP regex on mention (#name) [duplicate]

This question already has answers here:
Finding #mentions in string
(6 answers)
Closed 5 years ago.
I have busting my balls on this one, and I have been trying numerous regex'es but just can't seem to get it right. (I am not that experienced in regex).
The following situation is going on, lets take this basic sentence for example;
I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?
The trick here is to get only the #john-doe and #foo-bar parts from the string, preferably in an array:
$arr = [
'#john-doe',
'#foo-bar'
];
Could someone help me get on the right track?
You can use this regex (\#(?P<name>[a-zA-Z\-\_]+)) :
<?php
$matches = [];
$text = "I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?";
preg_match_all ("(\#(?P<names>[a-zA-Z\-\_]+))" ,$text, $matches);
var_dump($matches['names']);
In this example, I used the ?P<names> to name the capture groups, it's easier to get it.
I've made a Regex101 for you, and a PHP sandbox for test
https://regex101.com/r/ZFWvCG/1
http://sandbox.onlinephpfunctions.com/code/1d04ce64a2a290994bf0effd7cf8f0039f20277b

PHP Regular expression - forget everything after? [duplicate]

This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]

add math operators in function php [duplicate]

This question already has answers here:
How to mathematically evaluate a string like "2-1" to produce "1"?
(9 answers)
Closed 9 years ago.
I'm kinda new to this but I was wondering if it is possible to add some dynamic calculation into a function as a parameter.
The thing is inside my function I am formatting stuff in a consistent way but each time I want to add a certain different calculation into the parameter.
<?php
function dynamicCalculator($calculation){
$result = $calculation;
//some formatting
return $result;
}
echo dynamicCalculator('(3x5)+1');
This doesn't work of course but if anyone has an idea how this could work I would love to hear it.
What you are looking for is the eval function.
eval('$result = (3*5)+1');
But beware to make sure you're not passing possibly harmful code to that function.
Your looking for RPN (Reverse Polish Notation)
Here is one example
http://pear.php.net/package/Math_RPN/
which would allow you to use
$expression = "(2^3)+sin(30)-(!4)+(3/4)";
$rpn = new Math_Rpn(); echo $rpn->calculate($expression,'deg',false);
And not have to use Eval
Use eval. eval("10+2") should return 12. Be careful though, you can also run PHP code with eval.
Someone else had a similar question on StackOverflow.
You could use this:
function dynamicCalculator($calculation) {
$result = eval($calculation);
return $result;
}

Categories