What is wrong with this PHP script?
Miscellaneous Forums/General Discussion/What is wrong with this PHP script? is the /% being converted to an escape code for something?
see if it works by taking the '[' out of the string... if so then it could have something to do with the '$' because that declares a variable..
I can't take the "[" out of the string, it is part of the string I need to use.
try replacing '$' with '\$'
the easier solution of course is to just use single quotes instead of double ones.
Brownie points: +1
Brownie points: +1
Thank you.
I need a list of all special characters that need to be "encoded" in a PHP string variable. So far I have found these:
" = \"
$ = \$
' = \'
What else is there?
I need a list of all special characters that need to be "encoded" in a PHP string variable. So far I have found these:
" = \"
$ = \$
' = \'
What else is there?
\n ------linefeed (LF or 0x0A (10) in ASCII)
\r ------carriage return (CR or 0x0D (13) in ASCII)
\t ------horizontal tab (HT or 0x09 (9) in ASCII)
\v ------vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f ------form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ ------backslash
\$ ------dollar sign
\" ------double-quote
\[0-7]{1,3} ------the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} ------the sequence of characters matching the regular expression is a character in hexadecimal notation
\r ------carriage return (CR or 0x0D (13) in ASCII)
\t ------horizontal tab (HT or 0x09 (9) in ASCII)
\v ------vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f ------form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ ------backslash
\$ ------dollar sign
\" ------double-quote
\[0-7]{1,3} ------the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} ------the sequence of characters matching the regular expression is a character in hexadecimal notation
Well I was almost there :)
I'd recommend using single quotes rather than double qotes for random strings, as the single quotes expand fewer string combinations. More info below.
Single quoted
The easiest way to specify a simple string is to enclose it in single quotes (the character ').
To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself.
Note: In PHP 3, a warning will be issued at the E_NOTICE level when this happens.
Note: Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
For escaping problem characters in double quoted strings, you might want to take a look at the 'quotemeta' PHP function.
Description
string quotemeta ( string str )
Returns a version of str with a backslash character (\) before every character that is among these: . \ + * ? [ ^ ] ( $ )
Note: This function is binary-safe.
Single quoted
The easiest way to specify a simple string is to enclose it in single quotes (the character ').
To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself.
Note: In PHP 3, a warning will be issued at the E_NOTICE level when this happens.
Note: Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
For escaping problem characters in double quoted strings, you might want to take a look at the 'quotemeta' PHP function.
Description
string quotemeta ( string str )
Returns a version of str with a backslash character (\) before every character that is among these: . \ + * ? [ ^ ] ( $ )
Note: This function is binary-safe.
@Bill, I've said that already. :)
Ah. Whoops!
Never mind Bill, it's somewhat of a tradition here to repeat the same information within a thread at least three times.
Josh, use single quotes instead...Now where do I collect my brownie points?
;o)
Josh, use single quotes instead...Now where do I collect my brownie points?
;o)
I don't understand how this quotemeta thing is supposed to work. It accepts a string. But the whole problem is PHP is too stupid to handle a simple string:
<?PHP
$text=quotemeta ( ":z0dfdg%p?4rZ${[BoSnV`/%" );
echo $text;
/?>
<?PHP
$text=quotemeta ( ":z0dfdg%p?4rZ${[BoSnV`/%" );
echo $text;
/?>
use single quotes
Are you pressing the shift key?
It cuts out everything between < and > characters.
the code I posted works fine.
I did notice that for some reason you have a forward slash before the ?> in your code, I presume this was a typo on the forum?
I did notice that for some reason you have a forward slash before the ?> in your code, I presume this was a typo on the forum?
The code you posted does not include < and > characters.
Ahhh, I see what you're doing now.
Your problem here is that your browser is interpreting the <> as an HTML tag (an invalid one, which it then ignores).
if you want to echo the characters on screen, use
Your problem here is that your browser is interpreting the <> as an HTML tag (an invalid one, which it then ignores).
if you want to echo the characters on screen, use
echo htmlentities($text);
Parse error: syntax error, unexpected '[' in /home/content/L/e/a/Leadwerks/html/a.php on line 3
Whats this? Leadwerks, the hater of Linux, is using Linux for his server!?! I have had it with this, it doesn't work.
leadwerks, can you post the example that is causing you the problem? you didn't post an example including the < > tags. :)