SyntaxError: Unexpected token ILLEGAL caused by \u2028 and \u2029 - php

I am sending a sting as a parameter to a JavaScript function:
theJSFunction('say hi dude');
Chrome gives me SyntaxError: Unexpected token ILLEGAL so after research, I've found that the following whitespaces generates the error \u2028 and \u2029.
The problem is, the string posted to the function is printed via PHP and I need it to be printed via PHP (could use ajax, but I am required to let the PHP print it).
Is there any way to remove those to characters through PHP or JavaScript?

You can replace with .replace. Passing a regular expressions with the g flag replaces all occurences, and you can include \uxxxx characters as well:
"\u2028\u2029".replace(/\u2028|\u2029/g, "").length; // 0

Related

URL Decoding in PHP not working as supposed to be

I am trying to decode this URL string using PHP's urldecode function:
urldecode("%3CR201810579707%3E%20%3C20180828%3E%20%3C20180912%3E%20%3C1033.00%3E%20%3CY%3E%20%3C0.00%21NA%3E");
This is supposed to output...
<R201810579707> <20180828> <20180912> <1033.00> <Y> <0.00!NA>
...but instead is ouptutting this
<20180828> <20180912> <1033.00> <0.00!NA>
I've tested the string in a php online decoder with great success, but can't seem to do this operation server side. Any ideas?
If you're printing the result on a web page, the angle brackets will be treated as tag delimiters. You can display it literally by calling htmlentities():
echo htmlentities(urldecode("%3CR201810579707%3E%20%3C20180828%3E%20%3C20180912%3E%20%3C1033.00%3E%20%3CY%3E%20%3C0.00%21NA%3E"));

Firebase php invalid character value error

Using firebase php, I am getting following error in a simple $firebase->set($data,$location) function.
Uncaught exception 'Kreait\Firebase\Exception\FirebaseException' with
message 'The location key "rural foothills m.d." contains on of the
following invalid characters: .$#[]'
Is there a way to sanitize the value to avoid the error?
Thanks!
Sometimes you will see an error that says "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."
This error occurs when the fields you are trying to send to Firebase contain invalid characters. Firebase prohibits key names from containing:
. (period)
$ (dollar sign)
[ (left square bracket)
] (right square bracket)
# (hash or pound sign)
/ (forward slash)
To get around the error, you will need to manually assign names to the keys.
Reference: Common Problems with Firebase

preg_replace not working as expected replacing backslashes?

I was using the delimiters described in this answer:
Here is shell php I'm using to demonstrate the problem:
I'm using a function on a web project that replaces a namespace syntax for a path, eg:
\org\project\namespace\access
should be converted to:
/org/project/namespace/access
But I tried many ways, some threw the following warning:
Warning: preg_replace(): No ending delimiter '/' found in...
Which I don't want to show.
The warning above shows when using this as regex: /\\/
extra: (please look at the image) why it shows a bad encoded string?
UPDATE: why does it not work as this PHP Live Regex ?
You need to use "/\\\\/" instead of "/\\/" because \\ will produce \ (a single backslash) in a PHP string literal.
See http://php.net/manual/en/language.types.string.php
Why are you using regular expressions for such simple case? Stick to str_replace() or strtr():
echo strtr($str, ['\\' => '/']);

PHP Send multistring email

trying to use a EOD tag to send an email via PHP, heres my code:
The Code is too large, I pasted it on pastebin:
http://pastebin.com/KK3QZx6p
Dreamweaver has an error on line two and the actual browser reports an error on the last line (the script tag that includes jQuery)
I have tried backslashes and all, but the EOD tags are being syntax highlighted, is that maybe the issue?
I basically want to send an email, and the main body I want to be a html email, I know you can use the concatenation, although if there is a way to paste it in using a multiline string and send it, that would be great, although the EOD tag, simply isn't highlighting, like I have done something wrong
Error From Browser: Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in /home/a8526867/public_html/index.php
Dreamweaver is erroring on the second line
Indeed, I guess '{' at the last row gives a problem. You might try to escape it. But anyway, I don't think it's a good idea to use js or jquery in an email.

spacing between square brackets of an array

I have an array where I would like to put spaces between the [] like :
$array[South Africa]=array();
But I can't... why this is not possible?
The correct way of doing this is:
$array['South Africa'] = array();
By not placing quotes around strings, PHP will first check if it is a constant, and if not assume you want to specify the string stated (and generate a warning).
This would work without a space (other than the warning and being bad practise) but with the space PHP thinks the string/constant has ended after 'South' and expects an ]. What you have specified will result in a syntax error:
unexpected T_STRING, expecting ']'
I personally would avoid using spaces for names/keys anyway but the above explains the problem you are having if you must do this.

Categories