...

View Full Version : eregi with leading *



Ultragames
08-15-2005, 01:39 AM
If i do an eregi search with a leading *, it does not wild card properly.

Example:
eregi("^mfc*", "mfc8000") returns true.
eregi("^*mfc*", "Brothermfc8000") returns false, when it should be true.

What do i do?

marek_mar
08-15-2005, 01:49 AM
You should use preg_* functions and not ereg* functions.
The * is a quantifier not a wildcard.
If you just want to find a mfc in a string you dson't need regex:


if(stristr($string, 'mfc') === false)
{
print 'no mfc found';
}

Ultragames
08-15-2005, 01:52 AM
the string might have multiple wild cards.

Search: '*mfc*c'
String: 'brothermfc8000c'
Returns true.

How can this be acheived?

marek_mar
08-15-2005, 01:58 AM
How can this be acheived?
What?
The code I provided will find the mfc.

Ultragames
08-16-2005, 10:04 AM
Right, with my first search. But if you want multiple wild cards, seperating a string such as "mfc*AN" then that wont work. It needs to check that all sub strings in the search are in the string, and that they appear in that order.

NancyJ
08-16-2005, 10:28 AM
If i do an eregi search with a leading *, it does not wild card properly.

Example:
eregi("^mfc*", "mfc8000") returns true.
eregi("^*mfc*", "Brothermfc8000") returns false, when it should be true.

What do i do?
try [.]+mfc[.]+

Ultragames
08-16-2005, 10:46 AM
The [.]+ did not work. I did write code that does exactly what i want, though it is quite messy.


Note: The space was added to one, to fix an error in strpos, where a query at the begining of the string, will appear the same as if it was not found.

Note: I know about stripos, but i was forced to use strtolower because the server does not support stripos.


$One = "BROTHERmfc1000mc";
$Two = "*mfc*mc";
if( strstr($Two, '*') ){
// GET POSITION OF THE WILDCARDS
for($z=0; $z<strlen($Two); $z++){
$temp = substr($Two, $z, 1);
if( $temp == "*" ){
$Cards[] = $z;
}
}
$Cards[] = strlen($Two); // ADD AN ENDING MARKER FOR THE LOOP BELOW
if( in_array(0, $Cards) == true ){
$WildCardBegining = true;
} else {
$WildCardBegining = false;
}
print"WildCard positions:<br>"; print_r($Cards); print"<br>";

// PULL OUT THE STRINGS BETWEEN THE WILDCARDS
for($z=0; $z<sizeof($Cards); $z++){
if($z==0){
$temp = substr($Two, 0, $Cards[$z]);
if( !empty($temp) ){
$Parts[] = $temp;
}
} else {
$temp = substr($Two, ($Cards[$z-1]+1), ($Cards[$z]-($Cards[$z-1]+1)));
if( !empty($temp) ){
$Parts[] = $temp;
}
}
}
print"Search sections:<br>"; print_r($Parts); print"<br>";

print"Wild card at the begining: ". $WildCardBegining ."<br>";

// SEARCH FOR THE STRINGS IN THE QUERY, IN ORDER
$strposOne = " $One";
for($z=0; $z<sizeof($Parts); $z++){
$temp = strpos(strtolower($strposOne), strtolower($Parts[$z]));
if( $z==0 ){ // If searching the first part
if( $WildCardBegining == true ){
if( $temp >= 1 ){
$PartFound++;
}
}else{
if( $temp == 1 ){
$PartFound++;
}
}
} else {
if( $temp >= 1 ){
$PartFound++;
}
}
}

// IF ALL STRINGS WERE FOUND, AND IN ORDER, PASS THE TEST
if($PartFound == sizeof($Parts)){
print"$One fits!";
} else {
print"$One is no good.";
}
}

However, if there is an easier way to get this to work, please let me know.

NancyJ
08-16-2005, 11:09 AM
sorry... you dont need [] round single characters..doh! didnt think it would negatively affect it but I just tested it and .+mfc.+ works but [.]+mfc[.]+ doesnt

marek_mar
08-16-2005, 01:28 PM
That would be becouse the . is treated as a dot (not any character) in character calsses.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum