PHP String Extract

Among enormous amount of PHP functions, the string functions are more in number and very popular and heavily utilized. So, the operations made upon string input like string concatenation, implode / explode, calculating string length and etc can be performed by PHP string functions.

Among them, we are going to see about some of PHP string functions that are used to extract required part of a string input given. These functions are,

  1. substr() – This function extracts string with start, end limit given.
  2. mb_substr() – As like as substr(), except it works with high performance in security point of view, and handles multi byte operations.
  3. preg_match() – Unlike above two functions, this accepts regular expression format, to find match among input string.

Each of the above PHP string extract functions differ with the arguments to be passed to these functions, type of data returned and etc. We can see about these in details in the remaining part of this article.

php_string_extract

substr()

This function accepts an input string from which the required part of it is going to be extracted. And this function also accepts two limit parameters denoting start and end limit of the string extraction.

The syntax of this function is as shown below.

substr($input_string, $start_limit, $end_limit);

substr() must have first two parameters, whereas, the end_limit is optional. By default, the entire length of the string will be taken as end_limit.

The start and end limit accepts integer value of both sign, to specify either positive or negative limit. If the limit is positive then the extraction is done by starting from the beginning of the input string to fix the limit position. Otherwise it is done by calculating from the end of the string.

The following PHP program deals with the substr() function, to see the possible responses created depends upon the various combination of positive and negative limits set with.

<?php
$input_string = "String Extract";
echo substr($input_string,2)."<br/>";
echo substr($input_string,2,4)."<br/>";
echo substr($input_string,-2)."<br/>";
echo substr($input_string,-2,4)."<br/>";
echo substr($input_string,2,-4)."<br/>";
echo substr($input_string,-4,-2)."<br/>";
?>

Int the above program, let us see the last echo statement having both limit with negative sign. Since, the start limit is -4, it will be set by moving string pointer from the end of the string, there by it will start extracting from -4th character r, and end with –2nd character and returns subsctring ra. Similarly, all the echo statements returns appropriate substrings, on every substr() function invoke.

mb_substr()

This functions is similar as PHP substr(), but it works in secure manner and also has good performance with execution speed. The mb_ prefix of mb_subsrt() denotes that this function handles multi byte operations.

As like as substr(), this function counts characters position from 0 to string length. But, added to the set of arguments, that is, input_string, start and end limit, mb_substr() accepts an additional argument for character encoding.

This forth argument of this functions is also optional, like end_limit. By default, internal character encoding would have been chosen, if nothing is specified for this argument.

preg_match()

Unlike substr() and mb_substr(), the preg_match function in PHP is used to extract matches found with the given input string with respect to the pattern given in the form of regular expression. Using this pattern giving feature, we can make an extensive search among the given input string which will be effective than finding exact match with the given string.

preg_match() function is not only used to extract matches, but also used to check whether match is found or not. It returns 1 if the match is found, where as, other two PHP string extract functions mentioned above returns required substrings instead.

This function includes the following arguments.

  • Regex pattern.
  • Input string subject to what the pattern is to be matched.
  • output array into which the found matches will be inserted.
  • flag by default has the value 0. As of PHP version 4.3.0, PREG_OFFSET_CAPTURE flag is used to get string offset.
  • offset denotes the position on the input string from where the search is to be started.

The following program is shown as an example of the preg_match function.

<?php
preg_match("/[0-9]+/", "preg_match offset parameter is added since PHP 4.3.",$matches,PREG_OFFSET_CAPTURE,0);
print "<pre>";
print_r($matches);
print "</pre>";
?>

The regex pattern specified in the above PHP program is to search for the number into a given input string, and, this function is expected to return the found matches into an array. PHP print statement follows the preg_match() function call, prints the following array as an output to the browser.

Array
(
  [0] => Array
    (
      [0] => 4
      [1] => 48
    )
)

Since, we have send PREG_OFFSET_CAPTURE flag, the string match is returned by its offset as shown in the above output, that is, the number 4 is found in 48th position of the given input string.

This PHP code tutorial was published on July 2, 2013.

↑ Back to Top