All posts by Pramod T P

xpath()

It runs an XPath query on XML data

  • XPath is a major element in the XSLT standard.
  • XPath can be used to navigate through elements and attributes in an XML document.
  • XPath stands for XML Path Language
  • XPath uses “path like” syntax to identify and navigate nodes in an XML document
  • XPath contains over 200 built-in functions
  • XPath is a major element in the XSLT standard
  • XPath is a W3C recommendation

Example

<?php
$xml = simplexml_load_file(“test.xml”);
$xml->registerXPathNamespace(“msg”,”http://www.phpcodez.com”);
$result = $xml->xpath(“msg:web”);
?>

XML

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>

Share

substr_compare()

It compares two strings from a specified start position

This function returns:
0 – if the two strings are equal
<0 – if string1 (from startpos) is less than string2
>0 – if string1 (from startpos) is greater than string2
If length is equal or greater than length of string1, this function returns FALSE

Example

<?php
 echo substr_compare("phpcodexc","phpcode",0);
 ?>

Output

2

Share

soundex()

It calculates the  soundex key of a string

  • A soundex key is a four character long alphanumeric string that represent English pronunciation of a word.
  • The soundex() function can be used for spelling applications.
  • The soundex() function creates the same key for similar sounding words.

Example

<?php
 echo soundex("phpcode");
 ?>

Output

P123

Share

quotemeta()

The quotemeta() function adds backslashes in front of some predefined characters in a string.

This function can be used to escape characters with special meanings, such as ( ), [ ], and * in SQL.

This function is binary-safe.

The predefined characters are:

  • period (.)
  • backslash (\)
  • plus sign (+)
  • asterisk (*)
  • question mark (?)
  • brackets ([])
  • caret (^)
  • dollar sign ($)
  • parenthesis (())

Example

<?php
 echo quotemeta("php(code)'");
 ?>

Output

php(code)’

Share

metaphone()

It calculate the metaphone key of a string

  • A metaphone key represents how a string sounds if said by an English speaking person.
  • The metaphone() function can be used for spelling applications.
  • The metaphone() function creates the same key for similar sounding words.
  • The generated metaphone keys vary in length.

Example

<?php
 echo metaphone("phpcode");
 ?>

Output

FPKT

Share

htmlspecialchars()

It converts predefined characters to HTML entities.  The predefined characters are:

  • & (ampersand) becomes &amp;
  • ” (double quote) becomes &quot;
  • ‘ (single quote) becomes &#039;
  • < (less than) becomes &lt;
  • > (greater than) becomes &gt;

Example

<?php
 echo htmlspecialchars("<a href='http://phpcodez.comt'>phpcode</a>");
 ?>

Output

<a href=’http://phpcodez.comt’>phpcode</a>

Share

pathinfo

It returns information about a file path

Example

<?php
echo "<pre>";
print_r(pathinfo("test.ini"));
?>

Output

Array
(
[dirname] => .
[basename] => test.ini
[extension] => ini
[filename] => test
)

 

 

Share

move_uploaded_file

It moves an uploaded file to a new location. It is used to validate whether the contents of $_FILES[‘name’][‘tmp_name’] have really been uploaded via HTTP, and also save the contents into another folder.

Example

<?php
 move_uploaded_file($tmp_name, $_FILES["file"]["name"]) or die("Failed");
?>
Share

fpassthru

The fpassthru() function reads all data from the current position in an open file, until EOF, and writes the result to the output buffer.

This function returns the number of characters passed or FALSE on failure.

Example

<?php

$fp = @fopen(“test.txt”, “r”);

fpassthru($fp);

?>

Share

each

It return the current key and value pair from an array and advance the array cursor

Example

<?php
 echo "<pre>";
 $array = array("php", "asp", "jsp", "as", "js");
 $array2 = each($array);
 print_r($array2);
 ?>

Output

Array
(
[1] => php
[value] => php
[0] => 0
[key] => 0
)

Share