communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "string handling" - download "string in string"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

string handling - string in string was created/last modified on Wed 17 Mar 2004 6:39 AM

<?php
   
   
// InStr function
   // checks for an occurance of a string
   // within another string
   
function InStr($String,$Find,$CaseSensitive = false) {
    
$i=0;
    while (
strlen($String)>=$i) {
     unset(
$substring);
     if (
$CaseSensitive) {
      
$Find=strtolower($Find);
      
$String=strtolower($String);
     }
     
$substring=substr($String,$i,strlen($Find));
     if (
$substring==$Find) return true;
     
$i++;
    }
    return
false;
   }

   
// A similar function, returns the number of occurances
   
function InStrCount($String,$Find,$CaseSensitive = false) {
    
$i=0;
    
$x=0;
    while (
strlen($String)>=$i) {
     unset(
$substring);
     if (
$CaseSensitive) {
      
$Find=strtolower($Find);
      
$String=strtolower($String);
     }
     
$substring=substr($String,$i,strlen($Find));
     if (
$substring==$Find) $x++;
     
$i++;
    }
    return
$x;
   }

   
// Another similar function, this will return the position of
   // the string. returns -1 if the string does not exist
   
function InStrPos($String,$Find,$CaseSensitive = false) {
    
$i=0;
    while (
strlen($String)>=$i) {
     unset(
$substring);
     if (
$CaseSensitive) {
      
$Find=strtolower($Find);
      
$String=strtolower($String);
     }
     
$substring=substr($String,$i,strlen($Find));
     if (
$substring==$Find) return $i;
     
$i++;
    }
    return -
1;
   }
  
  
?>
  

learn more about some of the PHP functions used in this snippet: for, function, return, strlen, strtolower, substr, unset, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

go to PHParadise to get free PHP, Javascript and FlashMX code