You are here: Home Programming PHP

PHP

 

 

 

How to use chr() Function in PHP?

The chr() function returns a character from the specified ASCII value.

Syntax

chr(ascii)

The "ascii"parameter can be specified in decimal, octal, or hex values. Octal values are defined by a leading 0, while hex values are defined by a leading 0x.

Problem

<?php
echo chr(52)."<br />";
echo chr(052)."<br />";
echo chr(0x52)."<br />";
?>

Solution

4
*
R

Share this post

Add a comment HTML Codes
opinions powered by SendLove.to

 

 

How to use PHP chop() Function

 

The chop() function will remove a white space or other predefined character from the right end of a string. Specifies which characters to remove from the string.
The following characters are allowed and is set to be removed if the charlist parameter is empty:

  • "\0" - ASCII 0, NULL
  • "\t" - ASCII 9, a tab
  • "\n" - ASCII 10, a new line
  • "\x0B" - ASCII 11, a vertical tab.
  • "\r" - ASCII 13, a carriage return
  • " " - ASCII 32, an ordinary white space

Syntax

chop(string,charlist)

 

Problem

<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?> 

Solution

Hello World!  Hello World!

Share this post

Add a comment

 

 

 

How to use PHP bin2hex() Function?

 

The bin2hex() function converts a string of ASCII characters to hexadecimal values. The string can be converted back using the pack() function.

 

Syntax

 bin2hex(string) 

 

Problem

<?php
 $str = "Hello world!";
 echo bin2hex($str) . "<br />";
 echo pack("H*",bin2hex($str)) . "<br />";
 ?>

Solution

48656c6c6f20776f726c6421
Hello world!

Share this post

Add a comment

 

 

 

How to use PHP addslashes () Function

The addslashes() function returns a string with backslashes in front of predefined characters. PHP runs addslashes() on all GET, POST, and COOKIE data by default. Therefore you should not use addslashes() on strings that have already been escaped, this will cause double escaping. The function get_magic_quotes_gpc() can be used to check this.

 

This function can be used to prepare a string for storage in a database and database queries.

 

The predefined characters  are:
- single quote (')
- double quote (")
- backslash (\)
- NULL

Syntax

addslashes(string)

Parameter

String is a required field

 

Problem

<?php
$str = "Who's Khabza?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";
?>

 

Solution

Who's Khabza? This is not safe in a database query.
Who\'s Khabza? This is safe in a database query.

 

 

 

Share this post

Add a comment

The addcslashes() function returns a string with backslashes in front of the specified characters.

Syntax

addcslashes(string,characters)

Parameter
string is a required. Specifies the string to check
characters is a required. Specifies the characters or range of characters to be affected by addcslashes()

 

Note: Be careful using addcslashes() on 0, r, n and t. In PHP, \0, \r, \n and \t are predefined escape sequences.

 

Problem

We will add backslashes to certain characters in a string: add php openinh tag and closing tag to following code,

 

$str = "Hello, my name is Khabza.";
 echo $str."<br \>";
 echo addcslashes($str,'m')."<br \>";
 echo addcslashes($str,'K')."<br \>";

 

Solution

Hello, my name iskhabza.
Hello, \my na\me is Kabza.
Hello, my name is \Kabza.

Share this post

Add a comment
Back to top
SA Topsites :: Simphiwe - Find me on Bloggers.com

Copyright © Green IT Web - Blog by Khabza in Cape Town 2012

Template by Joomla 1.7 Templates & Szablony Joomla.