html entities and angle brace issue - php

So, I have this rel/URL I am trying to stuff into a variable so I can print it out elsewhere:
$relnext = "<link rel='next'
href='javascript:".$content_pager->PagerName
. "_form."
. $content_pager->PagerName
. "PagerPage.value=\""
. $content_pager->Page+1
. "\"; "
. $content_pager->PagerName
. "DoSubmit();' />";
As it is, when I print it out, all I get is:
1"; MediaBoxContentDoSubmit();' />
After some research it 'appears' that I should use htmlentities, but:
echo htmlentities($relnext);
also just produces:
1"; MediaBoxContentDoSubmit();' />
Is there some other function that should be used here?
Thanks very much for any help you can give!

You have an operator precedence / associativity problem. The . operator to the left of the + operator execute before the + operator, because they are all left-associative and have the same precedence. You want the + operator (in $content_pager->Page+1) to execute first, then all the . operators.
As it is, you are adding a string to a number (1) with the + operator, in which case the string (everything before the +) will be taken as 0. That is why the first character is a 1, because it is the result of "some string"+1, which is interpreted as 0+1.
So, your first snippet should be:
$relnext = "<link rel='next' href='javascript: " . $content_pager->PagerName . "_form." . $content_pager->PagerName . "PagerPage.value=\"" . ($content_pager->Page+1) . "\"; " . $content_pager->PagerName . "DoSubmit();' />";
Note that the $content_pager->Page+1 part is now in parentheses.
More information:
http://php.net/manual/en/language.operators.precedence.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)

Escaping minus sign in PHP echo statement

I'm sure I'm missing something obvious here: the following is echoing lat-long variables from MySQL, and the longitude variable begins with a minus sign, which prevents the echo statement from reading it and all that follows it. I'm sure there is a way to clean/escape that but just can't work it out.
echo "http://maps.google.com/maps?ll=" . $row['latitude'] . "," . $row['longitude'] . " target=_new>View in Google Maps";
This is output from a PDO query and testing passing the lat-long into Google Maps.
As I understand, it's a link?
Then, use urlencode for string.
The minus signs are not a problem. You may need to urlencode() because of the comma, but you need quotes around the URL in the href as well:
echo '<br /><a href="http://maps.google.com/maps?ll='
. urlencode($row['latitude'] . ',' . $row['longitude'])
. '" target="_new">View in Google Maps</a>';

How do you show double quotes in single quotes PHP

I have a PHP echo statement:
echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address']. ",". $row['City']. "," . $row['State']. " 0". $row['ZipCode']. "," . $row['PhoneNumber']. ",". $row['Lattitude']. ",".$row['Longitude']. "]". ";<br>";
which outputs:
stores[0] = [The Ale 'N 'Wich Pub , 246 Hamilton St ,New Brunswick,NJ 08901,732-745-9496 ,40.4964198,-74.4561079];
BUT I WOULD LIKE THE OUTPUT IN DOUBLE QUOTES SUCH AS:
stores[0]=["The Ale 'N 'Wich Pub", "246 Hamilton St, New Brunswick, NJ 08901", "732-745-9496 Specialty: Sport", "40.4964198", "-74.4561079"];
I Have looked at the PHP String Functions Manual on PHP site but still don't understand how i can implement it. Your help is appreciated.
The keyword you miss is "escaping" (see Wiki). Simplest example:
echo "\"";
would output:
"
EDIT
Basic explanation is - if you want to put double quote in double quote terminated string you MUST escape it, otherwise you got the syntax error.
Example:
echo "foo"bar";
^
+- this terminates your string at that position so remaining bar"
causes syntax error.
To avoid, you need to escape your double quote:
echo "foo\"bar";
^
+- this means the NEXT character should be processed AS IS, w/o applying
any special meaning to it, even if it normally has such. But now, it is
stripped out of its power and it is just bare double quote.
So your (it's part of the string, but you should get the point and do the rest yourself):
echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address'] .
should be:
echo "stores[".$row['BarID']."] = [\"". $row['BarName'] . "\", \"" . $row['Address']. "\"
and so on.

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