Everyday string functions

A handful of PHP functions covers the text work of an everyday page: find a piece, change it, cut it into parts and put a line together.

str_contains and strpos

Start with search. Modern PHP offers str_contains for a simple true or false answer.

Use strpos when you also need the position. Both functions expect the full text first and the needle second.

<?php $text = "Hello PHP course"; var_dump(str_contains($text, "PHP")); var_dump(strpos($text, "course"));

php search.php

bool(true)
int(10)

The needle is in the text, so the first answer is true; the word course starts at offset 10, counted from zero. Both calls are case-sensitive, so "php" would not be found in this text.

str_replace and trim

Use str_replace to build a new string with all matches replaced.

Use trim to remove spaces and line breaks from both ends. Both functions return a new string and leave the original value unchanged.

<?php $text = " hello php "; var_dump(trim($text)); var_dump(str_replace("php", "PHP", $text)); var_dump($text);

php change.php

string(9) "hello php"
string(13) " hello PHP "
string(13) " hello php "

The trimmed value loses its two spaces at each end, the replacement keeps them because it works on the original string, and the third line proves that the original still holds what it held before the two calls.

explode and implode

Use explode to split a string by a delimiter into an array.

Use implode to join an array back into one string with a glue string. Counting the parts tells you how many items were found.

<?php $csv = "red,green,blue"; $parts = explode(",", $csv); echo implode(" - ", $parts), PHP_EOL; echo count($parts), PHP_EOL;

php split.php

red - green - blue
3

The comma is gone after the split, so the glue of the join is what separates the words again. count() reports the three pieces of the array.

sprintf basics

Use sprintf when a string mixes text and values.

Placeholders like %s for text and %d for whole numbers mark each slot. The function returns the finished string ready for output.

<?php $name = "Ada"; $score = 42; echo sprintf("Name: %s Score: %d", $name, $score), PHP_EOL;

php format.php

Name: Ada Score: 42

Each placeholder takes the next argument in order, so the name fills %s and the score fills %d, and the text around them stays as it was written.

Exercises

  1. Search the text of search.php for "php" in lower case. What do the two functions return, and why?
  2. Print the offset of "PHP" in the same text and explain the number.
  3. Split a sentence of five words into an array, print the count, then join the words with a comma and a space.
  4. Print a line with sprintf that holds a name, an integer and a price with two decimals.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does str_contains($text, $needle) return?

    Answer

    true or false

    The function answers one question, whether the needle occurs in the text, so its result is a boolean.

  2. Which function gives the position of a needle in a string?

    Answer

    strpos()

    strpos returns the offset of the first occurrence, counted from zero.

  3. What does trim() remove?

    Answer

    Spaces and line breaks at both ends

    trim works on the ends of the string and leaves the text in the middle, spaces included, as it is.

  4. Which pair splits a string and joins an array?

    Answer

    explode() and implode()

    explode cuts a string at a delimiter, and implode puts the pieces back together with a glue.

  5. What does this program print? <?php var_dump(str_contains("Hello PHP", "php"));

    Answer

    bool(false)

    The search is case-sensitive, and the text holds PHP with capitals, so the lower-case needle is not found.

  6. What does this program print? <?php var_dump(strpos("Hello PHP", "PHP"));

    Answer

    int(6)

    The H of PHP sits at offset 6, since the five letters of Hello come first, and the position is counted from zero.

  7. What does this program print? <?php var_dump(str_contains("Hello", ""));

    Answer

    bool(true)

    An empty needle occurs at every position, so str_contains answers true.

  8. What does this program print? <?php $parts = explode(",", "a,b,c"); echo count($parts);

    Answer

    3

    Two commas cut the string into three pieces, so the array holds three elements.

  9. What does this program print? <?php $parts = explode(",", "a,b,c"); echo implode("+", $parts);

    Answer

    a+b+c

    implode joins the three pieces with the plus sign given as the glue.

  10. What does this program print? <?php echo sprintf("%s=%d", "n", 7);

    Answer

    n=7

    The first placeholder takes the string and the second the integer, so the line reads n=7.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev