Zend PHP 7 Certification – Strings – Extracting

This post covers the Extracting section of the Strings chapter when studying for the Zend PHP 7 Certification.

Some of the string functions for extracting characters in a given string can be seen below.

The substr() function returns part of a string. It takes three parameters.

  • String – The input string that must be one character or longer
  • Start – If start is non-negative, the returned string will start at the start’th position in string, counting from zero. If start is negative, the returned string will start at the start’th character from the end of string
  • Length – The length of a string returned. If length is given and is 0, FALSE or NULL, an empty string will be returned.
echo substr('abcdef', 1);  // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

The str_word_count() function returns information about words used in a string. This function also takes three parameters.

  • String – The string
  • Format – Specify the return value. 0 – returns the number of words found. 1 – returns an array containing all the words found inside the string. 2 – returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
  • Charlist – A list of additional characters which will be considered as ‘word’
$str = 'Hello World, how are you today?';
print_r(str_word_count($str, 0)); // Outputs: 6
print_r(str_word_count($str, 1)); // Outputs: Array ( [0] => Hello [1] => World [2] => how [3] => are [4] => you [5] => today )
print_r(str_word_count($str, 1, '?')); // Outputs: Array ( [0] => Hello [1] => World [2] => how [3] => are [4] => you [5] => today? )
print_r(str_word_count($str, 2)); // Outputs: Array ( [0] => Hello [6] => World [13] => how [17] => are [21] => you [25] => today )

To translate characters or replace substrings, use the strtr() function. It can take four parameters.

  • String – The string being translated
  • From – The string being translated to to
  • To – The string replacing from
  • Replace Pairs – May be used instead of to and from, in which case it’s an array in the form array(‘from’ => ‘to’, …).
echo strtr("Hilla Warld","ia","eo"); // Outputs: Hello World

$arr = array("Hello" => "Hi", "World" => "Earth");
echo strtr("Hello World",$arr); // Outputs: Hi Earth

Note that strtr() is case sensitive.

$arr = array("Hello" => "Hi", "World" => "Earth");
echo strtr("Hello world",$arr); // Outputs: Hi world

The strspn() function finds the length of the initial segment of a string consisting entirely of characters contained within a given mask. strspn() takes four parameters.

  • Subject – The string to be examined
  • Mask – The list of allowable characters
  • Start – The position in subject to start searching
  • Length – The length of the segment to examine
// subject does not start with any characters from mask
var_dump(strspn("foo", "o"));

// examine two characters from subject starting at offset 1
var_dump(strspn("foo", "o", 1, 2));

// examine one character from subject starting at offset 1
var_dump(strspn("foo", "o", 1, 1));

// Outputs:
int(0)
int(2)
int(1)

Using the strtok() function tokenises a string. It only take two arguments.

  • String – The string to be split up (tokenises) into smaller strings.
  • Token – The delimiter used.
$string = "This is\tan example\nstring";
$tok = strtok($string, " \n\t");

while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}

// Outputs:
Word=This
Word=is
Word=an
Word=example
Word=string

View the other sections:

Note: This article is based on PHP version 7.1.