MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

String Function Tutorial

without comments

These functions let you create substrings, manipulate strings, or collect information to parse strings. They’re organized alphabetically with a brief description and example.

chop()

The chop() function is an alias for the rtrim() function, which removes designated characters form the end of strings in a left-to-right reading pattern. Please cross-reference the rtrim() function.

The optional parameter has a default list of the following trailing characters:

  • ” ” (ASCII 32 [0x20]), an ordinary space.
  • “\t” (ASCII 9 [0x09]), a tab.
  • “\n” (ASCII 10 [0x0A]), a newline (line feed).
  • “\r” (ASCII 13 [0x0D]), a carriage return.
  • “\0” (ASCII 0 [0x00]), the NUL-byte.
  • “\x0B” (ASCII 11 [0x0B]), a vertical tab.

The chop() function has the following prototype:

string chop(string str [, character_list])

Here’s an example trimming default characters from the right side of the string where it removes the white space and new line character:

<?php
  $str = "Hello World! \n";
  print "[".chop($str)."]";
?>

It prints the following:

[Hello World!]

When you override the character list, you replace it. This example removes a curly brace from the end of the string:

<?php
  $str = "{Hello World!}";
  print chop($str,"{}");
?>

It prints the following:

{Hello World!

chr()

The chr() function returns a single character equivalent to an ASCII value. It requires one int value parameter that maps to an ASCII value, and returns a null when the actual parameter is outside the ASCII values 1 to 255.

The chr() function has the following prototype:

string chr(int ascii_value)

The following sample program uses the chr() function to covert an ASCII 65 to a capital A:

<?php
  $string = chr(65);
  print "[".$string."]\n";
?>

It prints the following:

[A]

The following demonstrates a null return value:

<?php
  if (is_null(chr(300)))
    print "[null value]\n";
?>

It produces the following standard out stream:

[null value]

explode()

The explode() function returns an array of numerically indexed strings. It has three parameters; the first two are mandatory and the third is optional. The first required parameter is a string separator, the second is the string to parse, and the third is an optional limit value. The limit value caps the number of elements returned in the array. The function returns false when the string separator is a null element string. PHP 5.1 introduces a negative limit value, which excludes that limit value from the end of the array of substrings.

The explode() function has the following prototype:

array explode(string separator , string str [, int limit])

The following sample program uses the explode() function with a white space separator:

<?php
  $str = "One Two Three";
  $list = explode(" ",$str)
  foreach($list as $name => $value)
  print "[".$name."][".$value."]\n";
?>

It produces the following standard output stream:

[0][One]
[1][Two]
[2][Three]

implode()

The implode() function returns a string built from an array. It has two mandatory parameters. The first is a glue string, or joining string. The second is the array to convert into a string.

The implode() function has the following prototype:

array implode(string glue, array element_list)

The following sample program uses the implode() function with a white space separator:

<?php
  $list = array("One","Two","Three");
  $str = implode(" ",$list);
  print "[".$str."]\n";
?>

It produces the following standard output stream:

[One Two Three]

join()

The join() function is an alias for the implode() function. Please cross-reference the join() function for complete details on how the join() function works.

The join() function has the following prototype:

string join(string glue [, array element_list])

The following sample program uses the join() function with a white space separator:

<?php
  $list = array("One","Two","Three");
  $str = join(" ",$list);
  print "[".$str."]\n";
?>

It produces the following standard output stream:

[One Two Three]

ltrim()

The ltrim() function removes designated characters form the beginning of strings in a left-to-right reading pattern.

The optional parameter has a default list of the following leading characters:

  • ” ” (ASCII 32 [0x20]), an ordinary space.
  • “\t” (ASCII 9 [0x09]), a tab.
  • “\n” (ASCII 10 [0x0A]), a newline (line feed).
  • “\r” (ASCII 13 [0x0D]), a carriage return.
  • “\0” (ASCII 0 [0x00]), the NUL-byte.
  • “\x0B” (ASCII 11 [0x0B]), a vertical tab.

The ltrim() function has the following prototype:

string ltrim(string str [, character_list])

Here’s an example trimming default characters from the left side of the string where it removes the new line and a white space character:

<?php
  $str = "\n Hello World!";
  print "[".ltrim($str)."]";
?>

It prints the following:

[Hello World!]

When you override the character list, you replace it. This example removes a curly brace from the beginning of the string:

<?php
  $str = "{Hello World!}";
  print ltrim($str,"{}");
?>

It prints the following:

Hello World!}

number_format()

The number_format() function takes a float data type and formats the number as a string. It has one mandatory and three optional parameters. The mandatory first parameter is the number. The optional second parameter is the number of decimal places, the third is the decimal format symbol (typically a period), and the fourth is the thousands symbol (typically a comma).

The number_format() function has the following prototype:

string number_format(float number
 [, int decimals
 [, string decimal_symbol
 [, string thousands_symbol]]])

The following sample program uses the number_format() function to format a number:

<?php
  $number = 123456789.01;
  $str = number_format($number,2,".",",");
  print "[".$str."]\n";
?>

It produces the following standard output stream:

[123,456,789.00]

ord()

The ord() function returns an ASCII value for a single character. It has one required parameter, which is a one-character string value.

The ord() function has the following prototype:

string ord(string characater)

The following sample program uses the ord() function to convert an A to an ASCII 65 value:

<?php
  $int = ord("A");
  print "[".$int."]\n";
?>

It produces the following standard output stream:

[65]

quotemeta()

The quotemeta() function places a back-quote in front of: . \ + * ? [ ^ ] ( $ ) symbols. It takes a single string parameter and returns a back-quoted string as a result.

The quotemeta() function has the following prototype:

string quotemeta(string str)

The following sample program uses the quotemeta() function to back-quote a symbol:

<?php
  $str = quotemeta("a * b = c");
  print "[".$str."]\n";

It produces the following standard output stream:

[a \* b = c]

rtrim()

The rtrim() function removes designated characters form the end of strings in a left-to-right reading pattern.

The optional parameter has a default list of the following trailing characters:

  • ” ” (ASCII 32 [0x20]), an ordinary space.
  • “\t” (ASCII 9 [0x09]), a tab.
  • “\n” (ASCII 10 [0x0A]), a newline (line feed).
  • “\r” (ASCII 13 [0x0D]), a carriage return.
  • “\0” (ASCII 0 [0x00]), the NUL-byte.
  • “\x0B” (ASCII 11 [0x0B]), a vertical tab.

The rtrim() function has the following prototype:

string rtrim(string str [, character_list])

Here’s an example trimming default characters from the right side of the string where it removes the white space and new line character:

<?php
  $str = "Hello World! \n";
  print "[".rtrim($str)."]";
?>

It prints the following:

[Hello World!]

When you override the character list, you replace it. This example removes a curly brace from the end of the string:

<?php
  $str = "{Hello World!}";
  print rtrim($str,"{}");
?>

It prints the following:

{Hello World!

strcasecmp()

The strcasecmp() function returns a sorting number based on a case-insensitive comparison operation. It has two mandatory parameters, which are case-sensitive strings to compare. When the first string is greater than the second, the function returns a positive number. When the first string is less than the second, the function returns a negative number. When both strings are equal, the function returns a zero.

The strcasecmp() function has the following prototype:

int strcasecmp(string str1
              ,string str2)

The following sample program uses the strcasecmp() function:

<?php
  $value = strcasecmp("One","one");
  print "[".$value."]\n";
?>

It returns an int value as described based on an insensitive case comparison:

[0]

strchr()

The strchr() function is an alias for the strstr() function. The stristr() function returns a substring from a case-insensitive substring comparison point to the end of the string. It has two mandatory parameters, which are case-insensitive strings. The first parameter is the string to search, or the haystack. The second parameter is the substring to look for in the string, also known as the needle. It returns a substring from the first occurrence of the needle in the haystack.

The strstr() function has the following prototype:

int strstr(string haystack
          ,string needle)

The following sample program uses the strstr() function:

<?php
  $value = strstr("Abstract","act");
  print "[".$value."]\n";
?>

It returns the needle because nothing follows the needle value:

[act]

strcmp()

The strcmp() function returns a sorting number based on a case-sensitive comparison operation. It has two mandatory parameters, which are case-sensitive strings to compare. When the first string is greater than the second, the function returns a positive number. When the first string is less than the second, the function returns a negative number. When both strings are equal, the function returns a zero.

The strcmp() function has the following prototype:

int strcmp(string str1
          ,string str2)

The following sample program uses the strcmp() function:

<?php
  $value = strcasecmp("One","one");
  print "[".$value."]\n";
?>

It produces the following standard output stream on finding the first string less than the second because the ASCII values of capital characters are lower than those of lowercase characters:

[-1]

strstr()

The strstr() function returns a substring from a case-sensitive substring comparison point to the end of the string. It has two mandatory parameters, which are case-sensitive strings. The first parameter is the string to search, or the haystack. The second parameter is the substring to look for in the string, also known as the needle. It returns a substring from the first occurrence of the needle in the haystack.

The strstr() function has the following prototype:

int strstr(string haystack
          ,string needle)

The following sample program uses the strstr() function:

<?php
  $value = strstr("Abstract","act");
  print "[".$value."]\n";
?>

It returns the needle because nothing follows the needle value:

[act]

Written by maclochlainn

November 17th, 2012 at 1:12 pm

Posted in Uncategorized

Leave a Reply