Monday 10 March 2014

String Position Function

string
String position function in php programming language. strpos function stands for string positions. What its function does it takes three arguments from that two are required the first arguments is the string we want to search in it the second argument is the keyword or character that we want to find with in string and the last argument and also optional argument from string where you want to start searching by default it will be zero potion starts with zero and then incremented.

Code for String Position Function (Screenshot1):-

strpos function
Screenshot1


<?php



$find = 'is';



$string = 'This is a string, and it is an example. ';



echo strpos($string, $find);



?>

Output (Screenshot2):-

string position function
Screenshot2
In this example we had a string here and i will include couple of keywords or characters in string for which we want to search for like This is a string, and it is an example. As you can see keyword is had repeat three times in string In this keyword is and then after this is and after it is. Now we can search from string for each word. For each word in string repeat to find out we had to loop through the string and search of it now in this example i will search for is position where it comes first so, it will be is in this will be the 2 position. I am putting is variable into the find variable. So, in strpos function first argument is $string and second argument the word we want to find from string which is here in variable $find so second argument is variable $find the third is optional argument which is by default zero and we echo out the result as you seen in output is the first position of is. Now what if we want to find each $find from the string for that we had to loop through the string. If we will pass third argument 3 it will start from word keyword this so output will be 5 because our second time is search is on position of 5 try yourself.

Code for string position with loop (Screenshot 3):-

php function string position
Screenshot3


<?php



$find = 'is';

$find_length = strlen($find);



$string = 'This is a string, and it is an example.';



while ($string_position = strpos($string,$find,$offset)) {

echo <strong>$find</strong>.'found at ',.$string_position.'<br>';

offset = $string_position + $find_length;

}

?>


Output (Sreenshot4) :-

string position php
Screenshot4
In example of string position function with while loop in php.In while loop we gonna serach while we continuely finding this is word in the string as soon as string found we carry on seraching and it can not found we can break the loop. In while loop we taking variable $string_position so we can assign variable values inside our while loop variable and we can constantly reassiging $string_position value $string_position = strpos function with parameters such as $string , $find which word we are finding and offset is variable in which we are increment the position of last found keyword to futher serach on.For that i had created string length function to find the string length. And as you can see the result very position ofkeyword is its echo out the result


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates