How to find a position of character in a string in PHP without using strpos function

 In this blog you are going to see how to find a position of character in a string in PHP without using strpos function.

To find the position of the character in a string is a very simple and fast process if we going to use the string function called strpos function, but in this, we have to find the character of string without using strpos.

String Split function

The main purpose of the string split function is to split the string into an array. Below the syntax for string split.

str_split(string,length)

There are two parameters used in string split, one parameter is a string which we have to split and another parameter is the length of each array element and it is optional if it is not defined then the default is considered as 1.

Suppose we have string "contextual" , and we want to find the position of 't' in that string. follow the below step and code.


<?php

$str = "contextual";

$arr = str_split($str);

echo "<pre>";

print_r($arr);

foreach ($arr as $po => $st) {

if($st == 't') {

$data[] = $po; 

}

}

print_r($data);

?>


Output


How to find a position of character in a string in PHP without using strpos function


No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...