VS Code Regex Find and Replace - php

with php8 I am now getting warnings on if statements with my variables.
Current
if ($var): echo $var; endif;
Updated Format
if (isset($var)): echo $var; endif;
I am looking for a regex expression to wrap all instances of if ($var) and if($var) to if (isset($var). the $var is not always var obviously so I think this could be accomplished with regex . Thanks!
replace: if \(\$(.*)\) with: if (isset($$$1))
This almost works but it wraps the entire if-statement if it begins with a $.

Related

PHP string within quotes next to character without space

I had a lot of trouble searching google for this and it never returned what I was looking for. It seems simple enough but I can't see how its done.
$string = "any";
$text = "$stringway";
echo $text;
output: anyway
Of course it doesn't work because it is looking for a string called $stringway when I actually want it to output the string called $string with the plain text "way" concatenated on the end. I therefore need some method to separate the string from the text without actually adding a space.
I'm sure there's an incredibly simple solution.
Simply enclose your variable $string within {}(braces) or you can use .(dot) concatenation operator
$string = "any";
$text = "{$string}way";
echo $text;
The reason to use {} braces is because when you write "$stringway" it will search for $stringway which leads to undefined variable and with braces it'll consider it as $string."way"
You mean this?
$string = "any";
$text = $string."way";
echo $text;
Use the string concatenation operator . to combine strings.

totally stuck on RegEx that works on test sandbox but doesn't work live

I'm totally stuck on this.
I wrote this RegEx pattern which is supposed to get the value of a string from a file whose contents I am getting with file_get_contents();
The RegEx I wrote is this:
/.*\$test_variable\s*=\s*'?(.*?)'?;.*/is
My function is:
function tbs_quick_setting_find($variable) {
global $savedsettings;
$setting = preg_replace("/.*\$".$variable."\s*=\s*'?(.*?)'?;.*/is",'$1',$savedsettings);
if (!empty($setting)) {
return $setting;
} else {
return false; // value is NOT set
}
}
Strangely, this works fine when tested, like here: http://regex101.com/r/wN4lJ3
but it does not work when I test it on my website, instead of the value of the variable I get the entire contents of the $savedsettings file.
Any help would be greatly appreciated.
Thanks
Matching a $ sign with PHP regex - How and why to escape
I usually put the pattern into single quotes, where a single \ should be enough to escape a $ as a literal. To match $ in a double-quoted regex-pattern things can be confusing.
All those variants should work:
1.) Using single quotes for the pattern, one \ should be enough to escape $ as a literal
'/.*\$'.$variable.'\s*=\s*\'?(.*?)\'?;.*/is'
2.) Imho using double quotes, you would have to double-escape the $ because inside a regex pattern it's a metacharater that matches position of string- or line-end, additionally it interpolates a variable inside double-quotes. Why tripple-escape it? No idea yet :-)
"/.*\\$".$variable."\s*=\s*'?(.*?)'?;.*/is"
3.) Whether you're using double or singe-quotes for the pattern, a safe way should always be to put the $ into a character class:
"/.*[$]".$variable."\s*=\s*'?(.*?)'?;.*/is"
Please feel free to comment/correct, where I'm wrong.
Try this:
function tbs_quick_setting_find($variable) {
global $savedsettings;
$re = "/.*\\\$" . $variable . "\s*=\s*'?(.*?)'?;.*/is";
$setting = preg_replace($re,'$1',$savedsettings);
if (!empty($setting)) {
return $setting;
} else {
return false; // value is NOT set
}
}
problem was here \\\$. We need to scape de the "\" and the "$" caracter for the expression work.
example:
echo "/.*\$".$variable."\s*=\s*'?(.*?)'?;.*/is";
will spit this expression /.*$test_variable\s*=\s*'?(.*?)'?;.*/is (don't match with anything)
The correct way would be:
echo "/.*\\\$".$variable."\s*=\s*'?(.*?)'?;.*/is";
which spits this expression /.*\$test_variable\s*=\s*'?(.*?)'?;.*/is (which match correctly)

PHP parser: braces around variables

I was wondering, how is the semantics of braces exactly defined
inside PHP? For instance, suppose we have defined:
$a = "foo";
then what are the differences among:
echo "${a}";
echo "{$a}";
that is, are there any circumstances where the placement of the
dollar sign outside the braces as opposed to within braces makes
a difference or is the result always the same (with braces used
to group just about anything)?
There are a lot of possibilities for braces (such as omitting them), and things get even more complicated when dealing with objects or arrays.
I prefer interpolation to concatenation, and I prefer to omit braces when not necessary. Sometimes, they are.
You cannot use object operators with ${} syntax. You must use {$...} when calling methods, or chaining operators (if you have only one operator such as to get a member, the braces may be omitted).
The ${} syntax can be used for variable variables:
$y = 'x';
$x = 'hello';
echo "${$y}"; //hello
The $$ syntax does not interpolate in a string, making ${} necessary for interpolation. You can also use strings (${'y'}) and even concatenate within a ${} block. However, variable variables can probably be considered a bad thing.
For arrays, either will work ${foo['bar']} vs. {$foo['bar']}. I prefer just $foo[bar] (for interpolation only -- outside of a string bar will be treated as a constant in that context).
The brackets delimit where the variable name ends; this example should speak for itself.
$a = "hi!"
echo "$afoo"; //$afoo is undefined
echo "${a}foo"; //hi!foo
echo "{$a}foo"; //hi!foo
Also, this should spit out a warning; you should use
${'a'}
Otherwise it will attempt to assume a is a constant.
Also you can use braces to get Char in the position $i of string $text:
$i=2;
$text="safi";
echo $text{$i}; // f

python regex to find php functions in files

I’m looking for a regular expression to match all functions blocks (from start to end) in php files. For example:
function test_function($var) {
if ($var == 'somethin') {
print 'hi';
}
etc.
}
I need the start offset and end offset of the block. What regex can I use?
It is very very complicated and can't be done with one regular expression.
You may think that you can easily match `the beginning of a function like this:
\bfunction\b\s+\S+[^\(](\s+)?\(.*?\)\s+\{
But you can't because what is if there is this in a code?
$string = "function myfunction() {}";
So you should search on everything what isn't quoted. So for excluding quoted strings you can use this regular expression:
(?:(?=(?:(?:\\.|"(?:\\.|[^"\\])*"|[^\\'"])*'(?:\\.|"(?:\\.|[^"'\\])*"|[^\\'])*')*(?:\\.|"(?:\\.|[^"\\])*"|[^\\'])*$)(?=(?:(?:\\.|'(?:\\.|[^'\\])*'|[^\\'"])*"(?:\\.|'(?:\\.|[^'"\\])*'|[^\\"])*")*(?:\\.|'(?:\\.|[^'\\])*'|[^\\"])*$)(?:\\.|[^\\'"]))+
The next thing you should do is counting all { and } because you need to know when the function stops and I can't think about any regular expression which can do this. So you need to do this with looping through.
Look at this: https://github.com/ramen/phply

echo inside if loop

I have a variable like this
$string = "0>0.1";
I want to evaluate this inside an if loop like this
if($string)
{
///something
}
but im getting an error if i am doing this. How to go about it
you do not understand how evaluations or strings work.
If you put anything in a string, it will not be evaluated, but simply contain that value from that moment on.
$string = "0>0.1"; // contains "0>0.1"
$bool = 0>0.1; // contains TRUE
The IF statement needs a boolean, not a string.
You can use eval to evaluate things in strings. Remember that eval is evil and any php code may be in it.
You got some awful advise with your duplicate. Here is a safer eval variant that will actually evaluate to an result:
$str = "0>0.1";
if (preg_replace('/^(\d+\.?\d*)([><])(\d+\.?\d*)$/e', "$1 $2 $3", $str))
{
Note that it would still return true for invalid strings like "1>2b". You would need a match and a separate expression evaluation for that. (Writing a faux mini parser here isn't difficult, but obviously overkill for your question.)

Categories