How to work with strings in PHP?
Hello everyone! I'm new to PHP and encountered a problem when working with strings. I need to perform several operations: trim whitespace, replace a substring, and check if a string contains a specific word. I tried using the trim(), str_replace(), and strpos() functions, but the results don't match expectations.
Here's an example of my code:
$string = ' Hello, world! This is a test string. ';\n$trimmed = trim($string);\necho $trimmed; // Outputs 'Hello, world! This is a test string.'\n\n$replaced = str_replace('world', 'PHP', $trimmed);\necho $replaced; // Outputs 'Hello, PHP! This is a test string.'\n\n$position = strpos($replaced, 'test');\nif ($position !== false) {\n echo 'Word found at position ' . $position;\n} else {