Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-10-2006, 10:08 PM   PM User | #1
xanderman
New Coder

 
Join Date: May 2006
Location: Pennsylvania, USA
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
xanderman is an unknown quantity at this point
Arrow inStr Function

well, i've found this useful for many things so i figured i'd post it

PHP Code:
function inStr ($needle$haystack)
{
  
$needlechars strlen($needle); //gets the number of characters in our needle
  
$i 0;
  for(
$i=0$i strlen($haystack); $i++) //creates a loop for the number of characters in our haystack
  
{
    if(
substr($haystack$i$needlechars) == $needle//checks to see if the needle is in this segment of the haystack
    
{
      return 
TRUE//if it is return true
    
}
  }
  return 
FALSE//if not, return false

Format for using.
PHP Code:
if(inStr("CodingForums""The best site ever is CodingForums"))
{
  echo 
"CodingForums is in this string";

xanderman is offline   Reply With Quote
Old 05-10-2006, 10:55 PM   PM User | #2
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Aint is the same as strstr() or strpos()?
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 05-10-2006, 11:10 PM   PM User | #3
xanderman
New Coder

 
Join Date: May 2006
Location: Pennsylvania, USA
Posts: 31
Thanks: 0
Thanked 4 Times in 4 Posts
xanderman is an unknown quantity at this point
not quite.

this checks to see if the needle is in the haystack, it dosent modify the needle or haystack in anyway
xanderman is offline   Reply With Quote
Old 05-11-2006, 12:59 AM   PM User | #4
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
It's pretty close. It does get around the 0 (match at first char)/false (no match) confusion that sometimes causes bugs with strpos, though. You could state this more succinctly as:
PHP Code:
function inStr($needle$haystack)
{
    return 
strpos($haystack$needle) !== false;

PS: More comments are not always better comments. If anyone needs guidance as to what 'return true' does, noting that it returns true isn't going to clarify it any.

Last edited by ralph l mayo; 05-11-2006 at 01:03 AM..
ralph l mayo is offline   Reply With Quote
Old 05-11-2006, 06:41 PM   PM User | #5
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Quote:
Originally Posted by xanderman
not quite.

this checks to see if the needle is in the haystack, it dosent modify the needle or haystack in anyway
Neither of the functions modify the arguments given to them.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 05-12-2006, 07:53 PM   PM User | #6
lavinpj1
Regular Coder

 
Join Date: Sep 2005
Posts: 394
Thanks: 1
Thanked 0 Times in 0 Posts
lavinpj1 is an unknown quantity at this point
Seems rather silly to me. StrPos is very very very fast. Think if you have 100,000 characters. StrPos would take a matter of seconds, your function would take a lot longer.

Code:
if(strpos("The best site ever is CodingForums", "CodingForums")) 
{ 
  echo "CodingForums is in this string"; 
}
lavinpj1 is offline   Reply With Quote
Old 05-15-2006, 12:09 AM   PM User | #7
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Quote:
Originally Posted by lavinpj1
Seems rather silly to me. StrPos is very very very fast. Think if you have 100,000 characters. StrPos would take a matter of seconds, your function would take a lot longer.

Code:
if(strpos("The best site ever is CodingForums", "CodingForums")) 
{ 
  echo "CodingForums is in this string"; 
}
Your example there demonstrated the wrong way to use the function as ralph l mayo mentioned above... Remember, when using strpos, if a match is found at offset 0 (starting from the start), 0 will be returned which will evaluate to false. You should use:

PHP Code:
<?php

if( false !== strpos"...""..." ) ){
   echo 
"...";
}

// Please forgive the dots... im very tired

?>
missing-score is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:22 PM.


Advertisement
Log in to turn off these ads.