Function strpos() is useful for find characters from some text or file.
Example how to find position some/one character from some text.
<?php
$mystring = ‘abcdef’;
$findme = ‘a’;
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of ‘a’ was the 0th (first) character.
if ($pos === false)
{
echo “The string ‘$findme’ was not found in the string ‘$mystring’”;
}
else
{
echo “The string ‘$findme’ was found in the string ‘$mystring’”;
echo ” and exists at position $pos”;
}
?>
Output :
The string ‘a’ was found in the string ‘abcdef’ and exists at position 0, because it start counting from 0
Other sample :
<?
$mystring = ‘abcdef bca‘;
$findme = ‘a’;
$pos = strpos($mystring, $findme, 1 );
if ($pos === false) {
echo “The string ‘$findme’ was not found in the string ‘$mystring’”;
} else {
echo “The string ‘$findme’ was found in the string ‘$mystring’”;
echo ” and exists at position $pos”;
}
?>
Output :
The string ‘a’ was found in the string ‘abcdef bca’ and exists at position 9
compare with this
<?
$mystring = ‘bacdef bca‘;
$findme  = ‘a’;
$pos = strpos($mystring, $findme, 1 );
if ($pos === false) {
echo “The string ‘$findme’ was not found in the string ‘$mystring’”;
} else {
echo “The string ‘$findme’ was found in the string ‘$mystring’”;
echo ” and exists at position $pos”;
}
?>
Output :
The string ‘a’ was found in the string ‘bacdef bca’ and exists at position 1
Other use :
<?php
$email1 = “info@forcescript.com”;
if (strpos($email1, “force“)) {
echo “Have force text”;
}
else {
echo “No force text”;
}
?>
Output :
Have force text
How to use for search position characters from some file ?
For File, we must use
fopen() for open file and fclose() for close file. For detail see at http://forcescript.com/php-file-handling.html
<?
$findme  = ‘testing’;
$file = fopen(“test.html”,”r”);
$skin = fread($file, filesize(“test.html”));
$pos = strpos($skin, $findme);
if ($pos === false)
{
echo “The string ‘$findme’ was not found”;
}
else
{
echo “The string ‘$findme’ was found”;
}
fclose($file);
?>