Jump to content

searching part of a database field?


adamhhh

Recommended Posts

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

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!  :)

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];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.