Jagdish Chaudhary, CSPO®️, CTO
2 min readJan 10, 2023

In native PHP, the string functions that respect double-dot range expressions (also known as “diamond operator” or “splat operator”) include:

  1. substr(): This function can be used to extract a portion of a string. It can accept two arguments: the string to extract from and the range of characters to remove (using the double-dot range expression). For example: substr('abcdef', 1..3) would return 'bc'.
  2. str_replace(): This function can replace all occurrences of a search string with a replacement string. It can accept three arguments: the search string, the replacement string, and the string to search in (using the double-dot range expression). For example: str_replace('x', 'y', 'axbxcxdxe'..'fxgxhx') would replace all occurrences of 'x' with 'y' in the range of strings from 'axbxcxdxe' to 'fxgxhx'.
  3. strpos(): This function returns the position of the first occurrence of a substring in a string. It can accept two arguments: the substring to search for, and the string to search in (using the double-dot range expression). For example: strpos('c', 'abcdef'..'ghijkl') would return 2

It’s important to note that the above examples are the uses of the splat operator in the context of string functions, it can be used in other ways to extract elements from an array, on which it can be used as an alternative to the array_slice method in PHP.

These are just a few examples of how the double-dot range expressions can be used with string functions in PHP, but there are many other possibilities depending on your specific use case.

addcslashes('adobe', 'a..e');
#OUTPUT : \a\do\b\e
chop('adobe', 'a..e');
#OUTPUT : ado
ltrim('adobe', 'a..e');
#OUTPUT : obe
rtrim('adobe', 'a..e');
#OUTPUT : ado
ucwords('laraveltips','l..s');
#OUTPUT : LarAvelTipS
trim('adobe', 'a..e');
#OUTPUT : o

Like and Clap for me :) Follow for more updates.

Jagdish Chaudhary, CSPO®️, CTO

PHP & JS Team Leader | DevOps | Web Architect | Full Stack | PHP, Laravel, CI, MySQL, PostgreSQL, SQL, Docker, K8, AWS, GCS, Azure, NodeJS, Socket, CICD