Case insensitivity

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Case insensitivity

Post by ozzy »

I have just put together a search script using glob...

Code: Select all

<?php
$term = trim($_POST['query']);
$files = glob($dir."modules/Exploits/files/*".$term."*");
foreach( $files as $key => $value ){
    echo $value."<br>";
} 
?>
But the problem is if i am seaching for a file called 'Test' and i type in 'test', it wont dispaly it, but if i type in 'Test' it will. I was thinking about using eregi, but i dont know how i would intigrate it into my script, could anyone help?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

strcasecmp(), strtolower(), and strtoupper() come to mind.

or preg_match() with the i pattern modifier.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Do you know how i would edit:

Code: Select all

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
   echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>
so that it would do the whole alphabet, rather than going through them separately?
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

Or the good 'ol ===?
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

someberry wrote:Or the good 'ol ===?
Pardon?
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

ozzy wrote:
someberry wrote:Or the good 'ol ===?
Pardon?

Code: Select all

$var = 'Hello';

if($var === 'hello')
{
	echo('if-statement #1 was executed.');
}
else if($var === 'Hello')
{
	echo('if-statement #2 was executed.');
}
Output:

Code: Select all

if-statement #2 was executed.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Ah cool, is there any way of using it to do the whole alphabet like a-z equal to A-Z rather than having to go through them separately?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ozzy wrote:Ah cool, is there any way of using it to do the whole alphabet like a-z equal to A-Z rather than having to go through them separately?
Can you post an example of what you mean? I have no idea what you're talking about.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

I know this wont be syntaxly correct, but it is justa example im using to try to get people to understand what i mean...

Instead of doing something like:

Code: Select all

$var1 = "a";
$var2 = "A"; 
$var3 = "b";
$var4 = "B"; 
$var5 = "c";
$var6 = "C";
ect.

Is there any way in doing it in one var, (not syntaxly correct, example):

Code: Select all

$var1 = "a-z";
$var2 = "A-Z";


Hope i have made it more clear.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I have no idea what you're trying to do with that.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$str = 'Test';
$str2 = 'test';

if(preg_match("#test#i",$str)){
   echo 'string matched';
}

if(preg_match("#test#i",$str)){
  echo 'string 2 matched';
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

or

Code: Select all

if(preg_match("[a-z]+#i",$your_data)){
  echo 'I will match any letter of the alphabet, capitalized or not.';
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Thanks scottayy, that's what i ment. :) But when added with my script:

Code: Select all

<?php

$term = trim($_POST['query']);

if(preg_match("[a-z]+#i",$term)){
}


$files = glob($dir."./*".$term."*");

foreach( $files as $key => $value ){

    echo $value."\n";

} 

?>
I get the following error:

Code: Select all

Warning: preg_match() [function.preg-match]: Unknown modifier '+' in /host/localhost/htdocs/test.php on line 5
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

sorry, I made a typo

Code: Select all

if(preg_match("[a-z]+#i",$term))
should be

Code: Select all

if(preg_match("#[a-z]+#i",$term))
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Ah, cool. Now comming back to the whole case insensitivity how would i peace that together with

Code: Select all

<form action="" method="POST"><input size="25" type="text" name="query" value="">&nbsp;<input type="submit" value="Search"></form>

<?php

$term = trim($_POST['query']);
}


$files = glob($dir."./*".$term."*");

foreach( $files as $key => $value ){

    echo $value."\n";

} 

?>
I have tryed

Code: Select all

<form action="" method="POST"><input size="25" type="text" name="query" value="">&nbsp;<input type="submit" value="Search"></form>

<?php

$term = trim($_POST['query']);
}

if(preg_match("#[a-z]+#i",$term))

$files = glob($dir."./*".$term."*");

foreach( $files as $key => $value ){

    echo $value."\n";

} 

?>
and

Code: Select all

<form action="" method="POST"><input size="25" type="text" name="query" value="">&nbsp;<input type="submit" value="Search"></form>

<?php

$term = trim($_POST['query']);
}

if(preg_match("#[a-z]+#i",$term))

$files = glob($dir."./*".$term."*");

foreach( $files as $key => $value ){

if(preg_match("#[a-z]+#i",$value))

    echo $value."\n";

} 

?>
But they didnt seem to work. :\
Post Reply