Open In App

How to read each character of a string in PHP ?

Last Updated : 21 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. 

Approach 1: Using str_split() method – The str_split() method is used to split the specified string variable into an array of values, each of which is mapped to an index value beginning with 0. This method converts the input string into an array.

str_split(str)

 PHP foreach loop iteration can then be done over the array values, each of which element belongs to a character of the string. The values are then printed with a space in between each. 

Example:

PHP




<?php
    
// Declaring string variable 
$str = "Hi!GFG User.";
  
echo("Original string : ");
echo($str . "</br>");
  
$array = str_split($str);
echo("Characters : ");
  
foreach($array as $val){
    echo($val . " ");
}
  
?>


Output:

Original string : Hi!GFG User.
Characters : H i ! G F G U s e r .

Approach 2: Using strlen() method – The strlen() method is used to compute the length of the specified string in PHP. A for loop iteration is applied using the length of the string, and the ith index character is printed each time. The time complexity is the same as the previous method. However, no extra space is required to store the string in the form of the array object. 

strlen(str)

Example:

PHP




<?php
    
// Declaring string variable 
$str = "Hi!GFG User.";
  
echo("Original string : ");
echo($str."</br>");
echo("Characters : ");
  
// Iterating over the string 
$len = strlen($str);
  
for ($i = 0; $i < $len; $i++){
    echo ($str[$i]." ");
}
  
?>


Output:

Original string : Hi!GFG User.
Characters : H i ! G F G U s e r .


Similar Reads

Create a Read More Read Less Button using HTML CSS and JavaScript
The "read more - read less" button is a commonly used feature that enhances user experience by providing a convenient way to reveal or hide content. The article covers two distinct approaches, including Display Inline and Display Block. Table of Content Using Display InlineUsing Display BlockUsing Display InlineCreate a separate HTML, CSS, and Java
4 min read
How to read if a checkbox is checked in PHP?
Using isset() Function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. This problem can be solved with the help of isset() fun
2 min read
How to read any request header in PHP
HTTP Header: HTTP headers are the code that transfers the data between web server and browser. The HTTP headers are mainly intended for the communication between the server and the client in both directions. HTTP Request Header: When type a URL in the address bar of browser and try to access it, the browser sends an HTTP request to the server. The
2 min read
PHP | Gmagick read() Function
The Gmagick::read() function is an inbuilt function in PHP which is used to read an image from your local folder and add it to the Gmagick object. Syntax: Gmagick Gmagick::read( string $filename ) Parameters: This function accepts a single parameter $filename which holds the name of the file. Return Value: This function returns a Gmagick object con
1 min read
PHP | XMLReader read() Function
The XMLReader::read() function is an inbuilt function in PHP which is used to move to next node in document. Thus this function is used to traverse through the XML document. Syntax: bool XMLReader::read( void ) Parameters: This function doesn’t accepts any parameter. Return Value: This function returns TRUE on success or FALSE on failure. Below giv
2 min read
How to read data from a file stored in XAMPP webserver using PHP ?
We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem. File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc fopen() function: The fopen() function in PHP is an inbu
2 min read
CRUD Operation in MySQL Using PHP, Volley Android - Read Data
In the previous article, we have performed the insert data operation. In this article, we will perform the Read data operation. Before performing this operation first of all we have to create a new PHP script for reading data from SQL Database. Prerequisite: You should be having Postman installed in your system to test this PHP script. Create a PHP
9 min read
How to read a Large File Line by Line in PHP ?
We will use some file operations to read a large file line by line and display it. Read a file: We will read the file by using fopen() function. This function is used to read and open a file. Syntax: fopen("filename", access_mode); Parameter: filename: Filename is the name of the file access_mode: It is the mode of the file that includes r - read m
2 min read
How to read PDF file using PHP ?
In this article, we will learn how you can show/read PDF file contents on a browser using PHP. We have to include an external PHP file named "class.pdf2text.php". Include it in the required web page using PHP. Create an HTML form, in which we can choose a PDF file from your computer and also check whether its file extension is PDF or not. Approach:
3 min read
How to Read and Write from Files in PHP ?
In PHP, reading from and writing to files is a common task in web development, especially for tasks such as reading configuration files, processing data files, or logging information. PHP provides built-in functions for handling file operations, making it straightforward to perform file read and write operations. Reading from a File:Use fopen() to
1 min read