PHP String Extract

by Vincy. Last modified on July 3rd, 2022.

Among the 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, calculating string length and etc can be performed by PHP string functions.

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

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

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

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 the start and end limit of the string extraction.

The syntax of this function is as shown below.

<?php
substr($input_string, $start_limit, $end_limit);
?>

substr() must have the 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 limits accept the integer value of both signs, to specify either a 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 depending upon the various combination of positive and negative limits set.

<?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/>";
?>

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

mb_substr()

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

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 fourth argument of this function 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 the regular expression. Using this pattern giving a feature, we can make an extensive search for the given input string which will be more effective than finding an exact match with the given string.

preg_match() function is not only used to extract matches but also used to check whether a match is found or not. It returns 1 if the match is found, whereas, the other two PHP string extract functions mentioned above return 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, the 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 in 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, and prints the following array as an output to the browser.

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

Since we have sent the 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 the 48th position of the given input string.

Comments to “PHP String Extract”

Leave a Reply to vinod sahare Cancel reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page