Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,698 questions

57,007 answers

573 users

How to find the position of the first occurrence of a substring in a string in PHP

7 Answers

0 votes
$string = 'XAMPP Control Panel Control';
$find   = 'Control';

$pos    = strpos($string, $find);

echo $pos;
 
 
/*
run:
 
6
 
*/

 



answered Jul 17, 2015 by avibootz
edited Apr 20, 2025 by avibootz
0 votes
$string = 'XAMPP Control Panel';
$find   = 'Control Power';
$pos    = strpos($string, $find);
  
if ($pos !== false)
    echo $pos;
else
    echo "string not found";


/*
run:

string not found

*/

 



answered Jul 21, 2015 by avibootz
edited Jul 23, 2015 by avibootz
0 votes
$string = 'XAMPP Control Panel';
$find   = 'o';

$pos    = strpos($string, $find);
   
if ($pos === false) {
    echo "string not found";
}
else {
    echo $pos;
}


 
/*
run:
 
7
 
*/

 



answered Jul 21, 2015 by avibootz
edited Apr 20, 2025 by avibootz
0 votes
$string = 'XAMPP Control Panel';
$find   = 'A';
$pos    = strpos($string, $find, 3); // srart search from char 3
  
if ($pos === false)
    echo "string not found";
else
    echo $pos;


/*
run:

string not found

*/

 



answered Jul 21, 2015 by avibootz
edited Jul 23, 2015 by avibootz
0 votes
$string = 'XAMPP Control Panel Apache + MySQL + PHP + Perl';
$find   = 'A';
$pos    = strpos($string, $find, 3); // srart search from char 3
  
if ($pos === false)
    echo "string not found";
else
    echo $pos;


/*
run:

20

*/

 



answered Jul 21, 2015 by avibootz
edited Jul 23, 2015 by avibootz
0 votes
$string = '1234567';
$find   = 1;  // 1 without apostrophes ''
$pos    = strpos($string, $find); 
  
if ($pos === false)
    echo "string not found";
else
    echo $pos;



/*
run:

string not found

*/

 



answered Jul 21, 2015 by avibootz
edited Jul 23, 2015 by avibootz
0 votes
$string = '8494029831';
$find   = '9'; 

$pos    = strpos($string, $find); 
   
if ($pos === false) {
    echo "string not found";
}
else {
    echo $pos;
}
 
 
/*
run:
 
2
 
*/

 



answered Jul 21, 2015 by avibootz
edited Apr 20, 2025 by avibootz
...