Search

PHP Strings

A string is series or collection of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.

  • A string is series or collection of characters
  • The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation marks 'hello word'

PHP String Example

<?php echo 'this is a simple string'; ?>

we will look at most commonly used functions to manipulate strings.

PHP String Functions

  • strtoupper() Return the upper case of a String
  • strtolower() Return the lower case of a String
  • strlen() - Return the Length of a String
  • str_word_count() - Count Words in a String
  • strrev() - Reverse a String
  • strpos() - Search For a Text Within a String
  • str_replace() - Replace Text Within a String

PHP strtoupper() Function

The strtoupper() function is used to convert a string into upper case. This function takes a string as parameter and converts all the lower case English alphabets present in the string to uppercase .

Syntax

strtoupper(string)

Example

<?php echo str_replace("Hello I am a Dev "); ?>

Out Put

//HELLO I AM A DEV

PHP strtolower() Function

The strtolower() function is used to convert a string into lower case. This function takes a string as parameter and converts all the uppercase English alphabets present in the string to lower case.

Syntax

strtolower(string)

Example

<?php echo strtolower("Hello I am a Dev "); ?>

Out Put

//hello i am a dev

PHP strlen() Function

The PHP strlen() predefine function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length.

Syntax

strlen(string)

Example

<?php echo strlen("Hello"); ?>

Out Put

// 5

PHP str_word_count() Function

The PHP str_word_count() predefine function in PHP which returns the total number of word in the string. It takes a string as a parameter and returns its number of word.

Syntax

str_word_count(string)

Example

<?php echo str_word_count("Hello World !"); ?>

Out Put

// 2

PHP strrev() Function

The PHP strrev() predefine function in PHP this is used to reverse a string

Syntax

strrev(string)

Example

<?php echo strrev("Hello World !"); ?>

Out Put

//!dlroW olleH

PHP strpos() Function

The PHP strpos() predefine function in PHP this function use to finds the position of the first occurrence of a string inside another string.

Syntax

strpos(string)

Example

<?php echo strpos("Hello World !","World"); ?>

Out Put

// 6

PHP str_replace() Function

The PHP str_replace() predefine function in PHP this function use to replaces some characters with some other characters in a string.

Syntax

str_replace(search,replace,string,count)

Example

<?php echo str_replace("_","-","this_is_url_string"); ?>

Out Put

//this-is-url-string

Stack Overlode is optimized Tutorials and examples for learning and training. Examples might be simplified to improve reading and learning and understand. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using Stack Overlode, you agree to have read and accepted our terms of use, cookie and privacy policy.