Usage analysis of substr of and explode of functions in PHP

  • 2021-08-05 09:18:39
  • OfStack

This article illustrates the use of substr () and explode () functions in PHP. Share it for your reference. The specific methods are as follows:

substr (string, start, length): This function takes out length characters from the string from the start bit of the string string. If start is negative, it will be counted from the end of the string. If the omitted parameter length exists, but it is negative, it means taking the penultimate length characters. The example code is as follows:

<?php 
echo substr("abcdef", 1, 3);  // Return "bcd"
echo substr("abcdef", -2);    // Return "ef"
echo substr("abcdef", -3, 1); // Return "d"
echo substr("abcdef", 1, -1); // Return "bcde"
?>

explode (separator, string, limit): This function divides the string into an array separator specifies where to divide, string divides the string, limit is optional, the maximum number of array elements, example code is as follows:
<?php 
  $str = "abc|bck|fhq|nui";
   print(explode("|",$srt)); // Output array([0]=>abc,[1]=>bck,[2]=>fhq,[3]=>nui)
?>

I hope this article is helpful to everyone's PHP programming.


Related articles: