Displaying Special Characters

Some characters are difficult to manipulate using php. Here are some examples how to use escape characters. (Updated: August 26, 2009)

 

PHP allows to use the backslash "\" character to display or manipulate special characters like the 'escape' codes, Also known as 'Escaping characters'.

 

For example, if we need to display the message:

 

"We need more supplies!" - He said.

 

We can use:
<?php
# Using the backslash for special characters
# Sample from www.josepino.com
echo "\"We need more supplies!\" - He said.";
?>
The backslash indicates to php to use the next character as a simple character and not for code purposes. Here is another example:
<?php
# this example displays:  "with quotes" or 'without quotes'
echo "\"with quotes\" or \'without quotes\'\r\n";
?>
Actually, it starts to be harder to understand using too many backslashes. What I do when I need to use those characters, I just try no to use backslashes. So I do write something like this:
<?php
# this example displays:  "with quotes" or 'without quotes'
echo '"with quotes" or ';
echo "'without quotes'";
?>
As I don't start the string with quotes on the first echo, I don't need to use the escape character. At the next echo instruction, I do use quotes to begin and end the string, so I can use the acute (') character.

 

Here is an example how I do use forms in HTML using php:
<html>
<head>
<title>PHP Sample using Forms and quotes</title>
</head>
<body>
<?php
# Sample from www.josepino.com
echo "<form action='http://josepino.com/form.htm' method='post'>\r\n";
echo "Email: <input type='text' name='email' size='30'>\r\n";
echo "<input type='submit' value='Submit'> \r\n";
?>
</form>
</body>
</html>

Instead of:

<html>
<head>
<title>PHP Sample using Forms and quotes</title>
</head>
<body>
<?php
# Sample from www.josepino.com
echo "<form action=\"http://josepino.com/form.htm\" method=\"post\">\r\n";
echo "Email: <input type=\"text\" name=\"email\" size=\"30\">\r\n";
echo "<input type=\"submit\" value=\"Submit\"> \r\n";
?>
</form>
</body>
</html>

It also works on this way:

<?php
echo '<form action="http://josepino.com/process.php" method="post">\r\n';
echo 'Email: <input type="text" name="email" size="30">\r\n';
echo '<input type="submit" value="Submit"> \r\n';
?>
The \r\n ending is used to insert a carriage return and new line at the html text so it can be viewed without any problem on linux-based and windows-based text editors.

 

Here are the most used escape codes on PHP:
Chr Meaning     Example:
\t  tab         echo ("separated\tby\ttabs");
\r  return      echo ("first line\r\nsecond line");
\n  new line    echo ("first line\r\nsecond line");
\\  backslash   echo ("Backslash: \\");
\$  dollar sign echo ("Total Amount: \$210.00");
\"  quotes      echo ("\"yes\" - he replied");
\[  left bracket
\]  right bracket
\{  left brace
\}  right brace echo ("\[ \] and \{ \} are used");

Octal values: from \0 to \777
Hexadecimal values: from \x00 to \xFF

echo ("Octal char 2 = \062 and Hex char 2 = \x32");

 


 < Creating MySQL UsersHomepage
scripts Index
How websites gets hacked.> 

 

If any information, data, picture or design infringes a copyrighted material, please send me an e-mail asking to remove it along with the supporting data.
Some features may not work with Google Chrome. © 2006-2010 Jose Pino - Powered by JPC Alpha