Lokang 

PHP and MySQL

String Functions

PHP provides a wide range of built-in functions for manipulating strings. Here are some commonly used string functions along with examples:

strlen - Get the Length of a String

<?php
$string = "Hello, World!";
$length = strlen($string);
echo $length; // Outputs: 13
?>

strpos - Find the Position of the First Occurrence of a Substring

<?php
$haystack = "Hello, World!";
$needle = "World";
$position = strpos($haystack, $needle);
echo $position; // Outputs: 7
?>

str_replace - Replace All Occurrences of the Search String with the Replacement String

<?php
$text = "Hello, World!";
$newText = str_replace("World", "PHP", $text);
echo $newText; // Outputs: Hello, PHP!
?>

substr - Return Part of a String

<?php
$string = "Hello, World!";
$substring = substr($string, 7, 5);
echo $substring; // Outputs: World
?>

strtolower - Convert a String to Lowercase

<?php
$string = "Hello, World!";
$lowercase = strtolower($string);
echo $lowercase; // Outputs: hello, world!
?>

strtoupper - Convert a String to Uppercase

<?php
$string = "Hello, World!";
$uppercase = strtoupper($string);
echo $uppercase; // Outputs: HELLO, WORLD!
?>

ucfirst - Capitalize the First Character of a String

<?php
$string = "hello, world!";
$capitalized = ucfirst($string);
echo $capitalized; // Outputs: Hello, world!
?>

lcfirst - Make the First Character of a String Lowercase

<?php
$string = "Hello, World!";
$lowercased = lcfirst($string);
echo $lowercased; // Outputs: hello, World!
?>

ucwords - Capitalize the First Character of Each Word in a String

<?php
$string = "hello, world!";
$capitalizedWords = ucwords($string);
echo $capitalizedWords; // Outputs: Hello, World!
?>

trim - Strip Whitespace (or Other Characters) from the Beginning and End of a String

<?php
$string = "  Hello, World!  ";
$trimmed = trim($string);
echo $trimmed; // Outputs: Hello, World!
?>

ltrim - Strip Whitespace (or Other Characters) from the Beginning of a String

<?php
$string = "  Hello, World!  ";
$trimmed = ltrim($string);
echo $trimmed; // Outputs: Hello, World!  
?>

rtrim - Strip Whitespace (or Other Characters) from the End of a String

<?php
$string = "  Hello, World!  ";
$trimmed = rtrim($string);
echo $trimmed; // Outputs:   Hello, World!
?>

explode - Split a String by a String

<?php
$string = "apple,banana,orange";
$array = explode(",", $string);
print_r($array);
// Outputs:
// Array
// (
//     [0] => apple
//     [1] => banana
//     [2] => orange
// )
?>

implode - Join Array Elements with a String

<?php
$array = ["apple", "banana", "orange"];
$string = implode(", ", $array);
echo $string; // Outputs: apple, banana, orange
?>

nl2br - Inserts HTML Line Breaks Before All Newlines in a String

<?php
$string = "Hello,\nWorld!";
$newString = nl2br($string);
echo $newString; // Outputs: Hello,<br />World!
?>

These examples cover many of the commonly used string functions in PHP. They can be used for various string manipulation tasks, from simple to complex operations.