PHP append to a new line in a file [duplicate] - php

This question already has answers here:
How to insert a new line into a CSV file?
(4 answers)
Closed 2 years ago.
This is my code in PHP:
$singles = fopen("singles.txt", "a");
$user_info = $name . "," . $gender . "," . $age . "," . $personality . "," .
$favOS . "," . $minAge . "," . $maxAge . PHP_EOL;
fwrite($singles, $user_info);
When I try to append the first time, is it appended to the same line but after that it is going to new line like I want.
As you see, New11 is added to the same line. I want it to be appended to a new line.
Also there is a trailing new line and I don't want an empty line at the end.

Just start the string with a newline, instead of finishing it with one. This should solve both issues - your first append will always start a new line, and your last one won't add a blank newline at the end.
$user_info = PHP_EOL . $name . "," . $gender . "," . $age . "," . $personality . "," .
$favOS . "," . $minAge . "," . $maxAge;

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)

combine 2 strings in one with some elements [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
The code looks like this:
$link .= '/'. . 'elevi' . .'/'. $clasa . '/' . $litera . '/' . $nume .'-'. $prenume;
But the error apear :
Parse error: syntax error, unexpected '.' in ...
But if the code looks like this :
$link .= $clasa . '/' . $litera . '/' . $nume .'-'. $prenume;
Works like this :
11/c/bobo-alex
and i want it to looks like this
./elevi/11/c/bobo-alex
with [dot] and slash first
You have to many string combining dots in your first segment.
You can also use "" instead of '' for PHP to auto-echo variable contents to the string.
$link .= "./elevi/$clasa/$litera/$nume-$prenume";
The dots are used to concatenate two strings, so you can't put two dots consecutive, it will rise a syntax error, so you can do it in two ways:
$mystring = './' . $var1 . '/' . $var2 . '/' . $var3 . '-' . $var4;
or:
$mystring = "./$var1/$var2/$var3-$var4";

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