adamhhh Posted March 8, 2007 Share Posted March 8, 2007 is there a way to do this? ive looked around and cant find much. What i have is a massive log file and ive tried extracting using Excel, Word etc but it doesnt work effectively. say in one database field i have: /index.php menu_id=1116 80 - 194.152.95.202 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+1.1.4322) 200 0 0 14775 526 I then need to extract and return just the menu_id=x and return x in a variable, any ideas how to go about this? i dont think i could use preg_replace as the data isnt consistent. Link to comment https://forums.phpfreaks.com/topic/41760-searching-part-of-a-database-field/ Share on other sites More sharing options...
adamhhh Posted March 8, 2007 Author Share Posted March 8, 2007 is there any way to do this as a regular expression, e.g. if (ereg ("(menu_id=)", $menuID, $menu_id)) { echo true; } ? where $menuID are fields from the database Link to comment https://forums.phpfreaks.com/topic/41760-searching-part-of-a-database-field/#findComment-202496 Share on other sites More sharing options...
rofl90 Posted March 8, 2007 Share Posted March 8, 2007 Could you explain in more detail Link to comment https://forums.phpfreaks.com/topic/41760-searching-part-of-a-database-field/#findComment-202502 Share on other sites More sharing options...
richardw Posted March 8, 2007 Share Posted March 8, 2007 Here is one method to parse the string, but it assumes that the id is always 4 digits, otherwise you will have to modify and count the second cut point searching for the first space. I am sure there is a simpler way to do this. <?php $menuID = "/index.php menu_id=1116 80 - 194.152.95.202"; $str_len = trim(strlen($menuID)); $str = $menuID; // $offset = 0; $i = 0; for ($i = 0; $i < $str_len; $i++) { $current_char = substr($str,$i,1); if ($current_char == "=" ) { $cutlength = $i; } else { } } $last = $str_len-$cutlength-3; $idout = (substr($menuID, $cutlength+1, $last-$cutlength)); echo "<br>id out for sample is ", $idout; ?> Best! Link to comment https://forums.phpfreaks.com/topic/41760-searching-part-of-a-database-field/#findComment-202605 Share on other sites More sharing options...
richardw Posted March 8, 2007 Share Posted March 8, 2007 I was curious about a simpler method... This also works: <?php $string = "/index.php menu_id=1116 80 - 194.152.95.202"; preg_match('/([\-0-9]+)(.*)/', $string, $string2); echo "<BR>"; echo "your id is ", $string2[number] = $string2[1]; ?> Link to comment https://forums.phpfreaks.com/topic/41760-searching-part-of-a-database-field/#findComment-202750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.