Page 1 of 1
finding a substring inside a string
Posted: Wed Apr 07, 2004 4:42 pm
by dsdsdsdsd
hello;
I would like to determine if substring exists within a string;
$substring = 'poiuy';
$string='_okmn_wwwwwww_poiuy_asasasasas';
is $substring inside of $string?
thank you
Shannon Burnett
Posted: Wed Apr 07, 2004 4:50 pm
by mudkicker
[php_man]ereg[/php_man] function or [php_man]substr_count[/php_man] function would help you.
Code: Select all
<?php
if(substr_count($text,$stringtofind) > 0) {
echo "yes it is in it.";
}
// or
if (eregi($stringtofind, $text)) {
echo "$text contains - $stringtofind!";
}
?>
Re: finding a substring inside a string
Posted: Wed Apr 07, 2004 4:51 pm
by qads
Code: Select all
<?php
$substring = 'poiuy';
$string='_okmn_wwwwwww_poiuy_asasasasas';
if(strstr($string, $substring))
{
echo 'String was found!';
}
?>
read up: [php_man]strstr[/php_man]
Posted: Wed Apr 07, 2004 4:51 pm
by feyd
Posted: Wed Apr 07, 2004 4:52 pm
by mudkicker
or strpos() function.
Posted: Wed Apr 07, 2004 4:57 pm
by qads
yep, more then 1 way to skin a cat as they say, you may wanna read up on string functions, just go to [php_man]strstr[/php_man] and you should get all the functions on th left hand side.
Posted: Wed Apr 07, 2004 6:40 pm
by dsdsdsdsd
I appreciate everyone's responses;
I had looked at these but was swayed away from them because their respective examples were seemingly all discussing single character needles;
I have already started a function that will parse each $string and $substring into arrays, determine first pos in string that matches first character in substring, then determines if all subsequent characters in $string match the subsequent characters in $substring;
I like strpos() better; I should have tested to see if needle could be a multi-character string a couple of hours ago;
thanks
dsdsdsdsd