a case insensitive version of the strpos() function?
15 replies, posted
Does anyone know of a case insensitive version of the strpos() function or anything that does close to the same thing? or do I have to program it in myself?
Use [URL="http://au2.php.net/manual/en/function.strtolower.php"]strtolower()[/URL] on the haystack and then use the value on the one with capitals.
[php]
$var = 'ASDFHKAJSDFasdfajsuihaasdf';
$pos = strpos(strtolower($var), 's');
echo 's found at '.$pos;
[/php]
Thank you... Wow I feel stupid. I know the srtolower function as well and didn't think about it.
Thank you again! I appreciate the help.
thanks swift and shift i just ran that and my website broke
[QUOTE=swift and shift;33547619]$index = 0;
for($i=0;$i<strlen($var);$i=$i+1){
if(substr($var,$i,1) == "a"){
$index = $i;
}
if(substr($var,$i,1) == "A"){
$index=$i;
}
}
hope that helps op xD ^_^[/QUOTE]
1. Why is everyone from Australia?
2. That's what I thought I had to do, but that would be kind of much for the code I was writing. I would have to write that every time needed or make a function which would be annoying.
EDIT:
Thank you though! The help is appreciated and I definitely wouldn't have been able to figure that out so quickly.
1. Australia is the best country in the world.
2. He was joking.
[QUOTE=Jelly;33547537]Use [URL="http://au2.php.net/manual/en/function.strtolower.php"]strtolower()[/URL] on the haystack and then use the value on the one with capitals.
[php]
$var = 'ASDFHKAJSDFasdfajsuihaasdf';
$pos = strpos(strtolower($var), 's');
echo 's found at '.$pos;
[/php][/QUOTE]
One last thing, How would I make the str_replace() function also case insensitive?
[QUOTE=Helpful stalker;33547689]One last thing, How would I make the str_replace() function also case insensitive?[/QUOTE]
[php]
$var = str_replace(array('a', 'A'), 'dix', 'ASDFHKAJSDFasdfajsuihaasdf');
[/php]
or
[php]
$var = str_replace(array('a', 'A'), array('dix', 'DIX'), 'ASDFHKAJSDFasdfajsuihaasdf');
[/php]
[QUOTE=Helpful stalker;33547689]One last thing, How would I make the str_replace() function also case insensitive?[/QUOTE]
this is a hard problem because you can't use strtolower because you might be lowering stuff you want to keep
so here is how i would do it
mysql_connect("localhost","root","your password here");
@mysql_select_db("your database here"); // the @ is so theres no error
$strStringToOperateOn="Hello World!";
$strStringToFind="world";
$strStringToReplace = "goodbye";
$intRunningIndex=0;
htmlspecialchars($strStringToOperateOn); //security
stripslashes($strStringToOperateOn);// so we cant be database injected
while(strpos("".strtolower("$strStringToOperateOn"),"".strtolower("$strStringToFind"))!==FALSE){
$strResult = "$strResult".substr("$strStringToOperateOn","$intRunningIndex",strpos("".strtolower("$strStringToOperateOn"),"".strtolower("$strStringToFind"))-"$intRunningIndex");
$strResult = "$strResult$strStringToReplace";
$intRunningIndex="$intRunningIndex";
$intRunningIndex+=strlen("$strStringToFind");
}
$strResult="$strResult".substr("$strStringToOperateOn","$intRunningIndex");
$strResult=(string)"$strResult"; // make sure it is a string
i hope that solves your doubt
[QUOTE=Jelly;33547698][php]
$var = str_replace(array('a', 'A'), 'dix', 'ASDFHKAJSDFasdfajsuihaasdf');
[/php]
or
[php]
$var = str_replace(array('a', 'A'), array('dix', 'DIX'), 'ASDFHKAJSDFasdfajsuihaasdf');
[/php][/QUOTE]
see but then I would have to program every possibility if they are full strings and that would be a bother. Is there any simpler less time consuming way?
For instance I will give you an example of what I am coding.
[php]
if(strpos(strtolower($_POST["message"]), "kill:") === 0 ) {
$_POST["message"] = str_replace("kill:", "", $_POST["message"]);
echo "You killed my website fucker, what do you have to say for yourself? \"" . $_POST["message"] . "\"";
}
[/php]
[php]
if(strpos(strtolower($_POST["message"]), "kill:") === 0 ) {
$_POST["message"] = str_ireplace("kill:", "", $_POST["message"]);
echo "You killed my website fucker, what do you have to say for yourself? \"" . $_POST["message"] . "\"";
}
[/php]
[url=http://www.php.net/manual/en/function.str-ireplace.php]str_ireplace()[/url] Probably should of used that in the first place.
I suppose I could just substr_replace() since I know the location of what I want removed every time.
[editline]3rd December 2011[/editline]
[QUOTE=Jelly;33547779][php]
if(strpos(strtolower($_POST["message"]), "kill:") === 0 ) {
$_POST["message"] = str_ireplace("kill:", "", $_POST["message"]);
echo "You killed my website fucker, what do you have to say for yourself? \"" . $_POST["message"] . "\"";
}
[/php]
[url=http://www.php.net/manual/en/function.str-ireplace.php]str_ireplace()[/url] Probably should of used that in the first place.[/QUOTE]
Awesome!
Thank you! I wasn't aware of this functions existence!
That's why I love PHP, so much to recycle.
[QUOTE=swift and shift;33547727]this is a hard problem because you can't use strtolower because you might be lowering stuff you want to keep
so here is how i would do it
mysql_connect("localhost","root","your password here");
@mysql_select_db("your database here"); // the @ is so theres no error
$strStringToOperateOn="Hello World!";
$strStringToFind="world";
$strStringToReplace = "goodbye";
$intRunningIndex=0;
htmlspecialchars($strStringToOperateOn); //security
stripslashes($strStringToOperateOn);// so we cant be database injected
while(strpos("".strtolower("$strStringToOperateOn"),"".strtolower("$strStringToFind"))!==FALSE){
$strResult = "$strResult".substr("$strStringToOperateOn","$intRunningIndex",strpos("".strtolower("$strStringToOperateOn"),"".strtolower("$strStringToFind"))-"$intRunningIndex");
$strResult = "$strResult$strStringToReplace";
$intRunningIndex="$intRunningIndex";
$intRunningIndex+=strlen("$strStringToFind");
}
$strResult="$strResult".substr("$strStringToOperateOn","$intRunningIndex");
$strResult=(string)"$strResult"; // make sure it is a string
i hope that solves your doubt[/QUOTE]
this is the cancer
[QUOTE=swift and shift;33547727]this is a hard problem because you can't use strtolower because you might be lowering stuff you want to keep
so here is how i would do it
mysql_connect("localhost","root","your password here");
@mysql_select_db("your database here"); // the @ is so theres no error
$strStringToOperateOn="Hello World!";
$strStringToFind="world";
$strStringToReplace = "goodbye";
$intRunningIndex=0;
htmlspecialchars($strStringToOperateOn); //security
stripslashes($strStringToOperateOn);// so we cant be database injected
while(strpos("".strtolower("$strStringToOperateOn"),"".strtolower("$strStringToFind"))!==FALSE){
$strResult = "$strResult".substr("$strStringToOperateOn","$intRunningIndex",strpos("".strtolower("$strStringToOperateOn"),"".strtolower("$strStringToFind"))-"$intRunningIndex");
$strResult = "$strResult$strStringToReplace";
$intRunningIndex="$intRunningIndex";
$intRunningIndex+=strlen("$strStringToFind");
}
$strResult="$strResult".substr("$strStringToOperateOn","$intRunningIndex");
$strResult=(string)"$strResult"; // make sure it is a string
i hope that solves your doubt[/QUOTE]
Ow, my liver.
there is stripos(), the insensitive version of strpos...
[url]http://php.net/manual/en/function.stripos.php[/url]
Sorry, you need to Log In to post a reply to this thread.