PHP - Modify Strings

0
3كيلو بايت

PHP has a set of built-in functions that you can use to modify strings.


Upper Case

ExampleGet your own PHP Server

The strtoupper() function returns the string in upper case:

$x = "Hello World!";
echo strtoupper($x);
Try it Yourself »

Lower Case

Example

The strtolower() function returns the string in lower case:

$x = "Hello World!";
echo strtolower($x);
Try it Yourself »

Replace String

The PHP str_replace() function replaces some characters with some other characters in a string.

Example

Replace the text "World" with "Dolly":

$x = "Hello World!";
echo str_replace("World", "Dolly", $x);
Try it Yourself »


Reverse a String

The PHP strrev() function reverses a string.

Example

Reverse the string "Hello World!":

$x = "Hello World!";
echo strrev($x);
Try it Yourself »

Remove Whitespace

Whitespace is the space before and/or after the actual text, and very often you want to remove this space.

Example

The trim() removes any whitespace from the beginning or the end:

$x = " Hello World! ";
echo trim($x);
Try it Yourself »

Learn more in our trim() Function Reference.


Convert String into Array

The PHP explode() function splits a string into an array.

The first parameter of the explode() function represents the "separator". The "separator" specifies where to split the string.

Note: The separator is required.

Example

Split the string into an array. Use the space character as separator:

$x = "Hello World!";
$y = explode(" ", $x);

//Use the print_r() function to display the result:
print_r($y);

/*
Result:
Array ( [0] => Hello [1] => World! )
*/
Try it Yourself »

Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.

 
 

 

 

البحث
الأقسام
إقرأ المزيد
أخرى
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
بواسطة PHP Tutorial 2024-05-17 07:09:40 0 2كيلو بايت
أخرى
PHP Math
PHP has a set of math functions that allows you to perform mathematical tasks on numbers. PHP...
بواسطة PHP Tutorial 2024-05-17 07:39:09 0 3كيلو بايت
أخرى
تطورات التعلم الآلي وتأثيرها على مستقبل العمل
مقدمة: في ظل التطورات السريعة للتكنولوجيا والابتكار، أصبح التعلم الآلي...
بواسطة MOHAMED ATTALLAH 2024-05-14 13:56:58 0 3كيلو بايت
أخرى
PHP Data Types
PHP Data Types Variables can store data of different types, and different data types can do...
بواسطة PHP Tutorial 2024-05-17 07:27:24 0 2كيلو بايت
أخرى
PHP Break
The break statement can be used to jump out of different kind of loops. Break in For...
بواسطة PHP Tutorial 2024-05-17 07:50:37 0 2كيلو بايت