preg_replace vs preg_replace_callback - php

I'm trying to update the code for some very old plugins for a very old blog. I've fixed almost everything except this.
I get an error message that I must replace preg_replace with preg_replace_callback.
This is the code:
$source_content = preg_replace($search.'e', "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
. $this->_quote_replace($this->right_delimiter)
. "'"
, $source_content);
If I simply substitute preg_replace_callback for the preg_replace I get this error:
preg_replace_callback(): Requires argument 2, ''{{php' . str_repeat(" ", substr_count('\0', " ")) .'}}'', to be a valid callback in
I'm neither a perl nor a php person. Any help would be much appreciated!

Upgrading your smarty library to the latest smarty2 would solve your problem fix was there over 5 years now.
But This might help you :
$source_content = preg_replace_callback($search . 'e',
function ($matches) {
return "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat(\"\n\", substr_count('$matches[0]', \"\n\")) .'"
. $this->_quote_replace($this->right_delimiter) . "'";
},
$source_content
);
Here is the explanition and more examples :
https://www.php.net/manual/en/function.preg-replace-callback.php

Related

PHP: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence

I am getting the following warning in my PHP script
The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence
The code works though, the PHP version on the server is: PHP 7.4.33, is there a problem with the code?
array_push($data, "" . str_getcsv($row)[0] . "," . str_getcsv($row)[1] . "," . str_getcsv($row)[2] . "," . str_getcsv($row)[3] + 1);
Try the this to explicitly set precedence, see if warning is clear.
array_push($data, "" . str_getcsv($row)[0] . "," . str_getcsv($row)[1] . "," . str_getcsv($row)[2] . "," . (str_getcsv($row)[3] + 1));

Match an actual dollar symbol in php regex

I haven't used regex in a long time and can't figure out how to match an actual dollar symbol and any reference to dollar symbol and regex tells me about the special meanings and cases. I need to match a $. I expected that \$ or $$ was supposed to escape it, but I'm still not matching it.
Here's my text
(WW) Capacity Charge
. . . . . . . . . . . . . . . . $ 123.45
WW Commodity Charge . . . . . . . . . $ 67.89
I'm trying to capture 123.45 I figured I should just match the first occurrence where some characters are sandwiched between the dollar symbol, space and a newline. Here are a few of the regexes I've tried.
preg_match("|(?<=\$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=\$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$$)(.*)(?=\n)|",$data[1],$matches); //no matches
Just to check that something matches I even did
preg_match("|(?<=\.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=1)(.*)(?=\n)|",$data[1],$matches); // 23.45
How can I match the text between the $ and the newline?
You are in double quotes so you need to escape twice (once for PHP, then once for PCRE). I prefer a character class though because that works in all regex flavors.
(?<=[$]\s)(.*)(?=\n)

Parse error: syntax error, unexpected '"' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I can's seem to figure out why I'm getting this error.
I need to turn user_link part into hyperlink(right now it outputs a text on the frontend). Error happens on this line in my shortcode.php file. I guess there is an error with a href part:
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' <a href='" . $order->user_link . "'>" . $order['user_link'] . "</a> </td>';
Your issue is right here:
. ' <a href='" .
Notice how you're starting with a single quote and ending with a double.
Definitely consider using an IDE. It will make these easy bugs blatantly obvious. I highly recommend PHPStorm, or if you don't want to spend anything, sublime text (Not an IDE but will provide linting and highlighting).
On top of that I'd recommend using some type of templating engine eventually. You should always try to avoid writing HTML as strings.
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' ' . $order['user_link'] . ' </td>';
You switched the ' and " in the a href. I also changed the two " to ' in the a description.

Getting a Syntax error, but I can't find the syntax error

So I'm working on an editor for a friend of mine, and I'm getting a strange Syntax error. It's strange because I'm currently creating an NPC editor using the shell of the Item editor I made a while back. That's saying I literally just changed the variables and changed everything that said 'item' to 'npc'. However, I'm getting a syntax error at a random column and I can't find out what the error is. It's in the editing section of the editor(lol). The delete and create parts of the editor work fine.
}else if($state == "edit")
{
$editsql = "UPDATE npcs SET name='" . $name . "', description='" . $description . "', gender=" . $gender . ", size=" . $size . ", dialog='" . $dialog . "', hair_style=" . $hair_style . ", hat=" . $hat . ", top=" . $top . ", bottom=" . $bottom . ", movement_pattern=" . $movement_pattern . ", behavior=" . $behavior . ", range=" . $range . ", uses_special_pokemon=" . $uses_special_pokemon . ", pokemon_1=" . $pokemon_1 . ", pokemon_2=" . $pokemon_2 . ", pokemon_3=" . $pokemon_3 . ", pokemon_4=" . $pokemon_4 . ", pokemon_5=" . $pokemon_5 . ", pokemon_6=" . $pokemon_6 . " WHERE id=" . $id;
this is the error:
Could not edit npc ID 3 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range=0, uses_special_pokemon=0, pokemon_1=1, pokemon_2=1, pokemon_3=1, pokemon_' at line 1
I can't quite figure out what it's calling out near 'range' and range itself looks fine to me, so I don't see an error at all. It's most likely something completely obvious that I'm just overlooking as usual, but I'm stumped.
You'll want to rename range to range_, because Range is a SQL reserved word. You could enclose it in backticks, which are different than single quotes. ` VS ' ...
If you seperate the query into multiple lines your error message will tell you where it failed closer to where the actual error was. It's a one-liner, so it tells you error exists on line 1. Typically, seperate clauses, i.e.
select xxxx
from yyyy
where xxxx = zzzz
then you'll know it's an error in syntax and in what clause.

Making a HTML Tagged email in php

I want to create an HTML Message to send an email in PHP.
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = <html><body><p>$message</p></body></html>;
Here are some variables.
I am getting the following error.
Parse error: syntax error, unexpected '<' in /home/thevowaa/public_html/iphoneapp/webservice/file.php on line 214
Add HTML tags between double quotes.
$message = "<html><body><p>".$message."</p></body></html>";
Where are the double quotes see below
$message = "<html><body><p>$message</p></body></html>";
There are two possible ways to hold HTML in PHP variable. You can use single quote or double quotes. You also need to put a dot(.) before and after single/double quotes. Your PHP string could be constructed in following two ways:
$message = '<html><body><p>'.$message.'</p></body></html>';
or like this,
$message = "<html><body><p>".$message."</p></body></html>";
Also, use of single quotes(') is encouraged in PHP coding because it's doesn't clash with javascript or css double quotes(") when constructing html pages using PHP.
For more information on usage of quotes in PHP, check out this stackoverflow answer

Categories