PHP String Function


length of string


The PHP strlen() function returns the length of a string.


	      		<?php
					echo strlen("Hello Students Empire!"); //output 22
				?>
	      

output


	      		22	      

Count The Number of Words in a String


The PHP str_word_count() function counts the number of words in a string:


	      		<?php
					echo str_word_count("Hello world!"); // outputs 2
				?>
	      

output


	      		2	      


Reverse a String


The PHP strrev() function reverses a string:


	      		<?php
					echo strrev("Hello world!"); // outputs !dlrow olleH
				?>
	      

output


	      		!dlrow olleH	      

strtolower


Used to convert all string characters to lower case letters


	      		<?php
					echo strtolower( 'Students Empire');
				?>
	      

output


	      		students empire	      

strtoupper


Used to convert all string characters to upper case letters


	      		<?php
					echo strtoupper( 'Students Empire');
				?>
	      

output


	      		STUDENTS EMPIRE	      

explode


Used to convert strings into an array variable


	      		<?php
					$settings = explode(';', "host=localhost; db=sales; uid=root; pwd=demo"); print_r($settings);
				?>
	      

output


	      		Array
(
    [0] => host=localhost
    [1] =>  db=sales
    [2] =>  uid=root
    [3] =>  pwd=demo
)
	      


substr


Used to return part of the string. It accepts three (3) basic parameters. The first one is the string to be shortened, the second parameter is the position of the starting point, and the third parameter is the number of characters to be returned.


	      		<?php
					$my_var = 'This is a really long sentence that I wish to cut short';echo substr
					($my_var,0, 12).'...';
				?>
	      

output


	      		This is a re...	      

sha1


Used to calculate the SHA-1 hash of a string value


	      		<?php
					echo sha1('password');
				?>
	      

output


	      		5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8	      

md5


Used to calculate the md5 hash of a string value


	      		<?php
					echo md5('password');
				?>
	      

output


	      		5f4dcc3b5aa765d61d8327deb882cf99	      

ucfirst


Make the first character of a string value upper case


	      		<?php
					echo ucfirst('hello world');
				?>
	      

output


	      		Hello world	      


lcfirst


Make the first character of a string value lower case


	      		<?php
					echo lcfirst('HELLO WORLD');
				?>
	      

output


	      		hELLO WORLD	      

Search For a Specific Text Within a String


The PHP strpos() function searches for a specific text within a string.


If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.


	      		<?php
					echo strpos("Hello world!", "world"); // outputs 6
				?>
	      

output


	      		6	      

Note: The first character position in a string is 0 (not 1).


Replace Text Within a String


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


	      		<?php
					echo str_replace("world", "Studentsempire", "Hello world!"); // outputs Hello Studentsempire!
				?>
	      

output


	      		Hello Studentsempire!	      

  • A string is a set of characters
  • single quotes are used to specify simple strings
  • double quotes are used to create fairly complex strings



PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion