function stipos($haystack, $needle){
return strpos($haystack, stristr( $haystack, $needle ));
}
function search_array ( $array, $term )
{
foreach ( $array as $key => $value )
if ( stipos( $value, $term ) !== false )
$val = str_replace('"',"",preg_replace("/[a-zA-Z0-9]=/","",$array[$key]));
if (isset($val)) return $val;
return false;
}
?>
I'm search for 7= and then expect to return the value associated to that. In this example, I'd expect : ID123456
However I have an additional entry in $line that is 27=\"Node\"
So the result I actually get is 2Node, not ID123456
I only want a match on 7= and NOT 17= or 27= which it's currently doing.
How do I change the to only return the value against 7= and not a mix of 27= & it's value ??
Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett
"But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html
I get a plain flat file and then read it in, I'm only selecting a few values from it so a DB is a bit of overkill.
Well you already have a database - I was simply offering that using a DBMS might be a more efficient way of managing that database.
Regardless, the method I had in mind above is more or less exactly what NogDog came up with, although you could also have utilized the fact that it appears you're being given "key=value" pairs (e.g. the value on the left of the equal sign will never be repeated within a row - it's a unique identifier). Example:
PHP Code:
// I'm assuming you actually have a file with many lines like the example string:
$file = [
'1="Device",3="User",7="ID1",27="Node"',
'1="Device",3="User",7="ID2",27="Node"',
'1="Device",3="User",7="ID3",27="Node"',
'1="Device",3="User",7="ID4",27="Node"'
];
// ... and then a loop to process all of them:
foreach($file as $line) {
if(preg_match_all('/(?:,|)([^=]+)="([^"]+)"(?:,|)/', $line, $data)) {
$data = array_combine($data[1], $data[2]);
print "Found row with ID: $data[7]!\n";
}
}
And of course the loop could be modified to instead store the data for later use if you didn't want to (or are unable to) process the data immediately.
Last edited by bradgrafelman; 02-13-2013 at 12:20 PM.
The \b sequence in a PCRE regex is a "word boundary" assertion, so \b7 will match on any "7" preceded by a white-space or any non-word character (I think the underscore is considered a word character in this case, but I'd have to check to be sure). The [^"]* is matching on 0 to n characters that are not a double quote, and wrapping that expression in parentheses makes it a sub-pattern that can be separately selected from the resulting $matches array.
Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett
"But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html
Bookmarks