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, 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,
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 details in the remaining part of this article.
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 accept 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 limits with a negative sign. Since, the start limit is -4, it will be set by moving 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 invoke.
This function is similar as 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.
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 fourth 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.
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 a feature, we can make an extensive search for the given input string which will be 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, other two PHP string extract functions mentioned above returns required substrings instead.
This function includes the following arguments.
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 (  => Array (  => 4  => 48 ) )
Since we have sent 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.