In PHP, how do I reduce strings with examples?

In PHP, you can reduce strings using various methods, such as using the `substr()`, `str_replace()`, or `preg_replace()` functions, among others. Below are some examples illustrating how to reduce strings effectively.

Keywords: String Reduction, PHP Strings, String Manipulation
Description: This content provides examples of how to reduce strings in PHP using different built-in functions.

Example 1: Using substr()

<?php $string = "Hello, World!"; // Reduce string to first 5 characters $reducedString = substr($string, 0, 5); echo $reducedString; // Output: Hello ?>

Example 2: Using str_replace()

<?php $string = "Hello, World!"; // Remove 'World' $reducedString = str_replace("World", "", $string); echo $reducedString; // Output: Hello, ! ?>

Example 3: Using preg_replace()

<?php $string = "Hello, World!"; // Remove everything except 'Hello' $reducedString = preg_replace('/[^Hello]/', '', $string); echo $reducedString; // Output: Hello ?>

Keywords: String Reduction PHP Strings String Manipulation