4 Replies - 360 Views - Last Post: 29 September 2011 - 07:20 PM Rate Topic: -----

#1 wildboy211  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-September 11

Best way to capture strings in strings.

Posted 29 September 2011 - 05:46 PM

I have users that when they do a certain thing on the website, it writes to a txt file something similiar to this:
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05


And if the system doesnt read the text file in time and another user does it, it will look like this:
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05 id=243097&type=test_event&signature=fe356ba60ee87277bdc7eab63e3637753c1fe3c9×tamp=2011-09-29%2020%3A52%3A41 


What would be the best way to send that to arrays like Array1 ([id] => 242810, [type] => "test_event",....etc etc
Im not really good at regex and im not sure if that would be the best way anyways, as i also thought of "parse_str" - but i cant figure out how to do it when there is more than one.

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to capture strings in strings.

#2 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Best way to capture strings in strings.

Posted 29 September 2011 - 06:15 PM

PHP - parse_str()

This will turn the line into an array or variables, your choice
parse_str('id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05',$array);

print_r($array);

Was This Post Helpful? 1
  • +
  • -

#3 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Best way to capture strings in strings.

Posted 29 September 2011 - 06:23 PM

Some examples, just threw them together so let me know how they do.
<?

#
#	Option for Single
#
$myString = 'id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05';
parse_str($myString,$array);
print_r($array);

#
#	Option for Multiple
#
$myString = 'id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05';
$myStrings = explode('id=',$myString);
$myArrays = array();
foreach($myStrings as $singleString)
{
	if(trim($singleString)!='')
	{
		$array = false;
		parse_str('id='.$singleString,$array);
		$myArrays[] = $array;
	}
}
print_r($myArrays);

#
#	Option for that will work with either single or multiple
#
$myString = 'id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05';
if(strrpos($myString,'id='))
{
	$myStrings = explode('id=',$myString);
	$myArrays = array();
	foreach($myStrings as $singleString)
	{
		if(trim($singleString)!='')
		{
			$array = false;
			parse_str('id='.$singleString,$array);
			$myArrays[] = $array;
		}
	}
	print_r($myArrays);
}
else
{
	parse_str($myString,$array);
	print_r($array);
}
?>

This post has been edited by VolcomMky: 29 September 2011 - 06:25 PM

Was This Post Helpful? 1
  • +
  • -

#4 wildboy211  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-September 11

Re: Best way to capture strings in strings.

Posted 29 September 2011 - 07:08 PM

View PostVolcomMky, on 29 September 2011 - 06:23 PM, said:

Some examples, just threw them together so let me know how they do.
<?

#
#	Option for Single
#
$myString = 'id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05';
parse_str($myString,$array);
print_r($array);

#
#	Option for Multiple
#
$myString = 'id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05';
$myStrings = explode('id=',$myString);
$myArrays = array();
foreach($myStrings as $singleString)
{
	if(trim($singleString)!='')
	{
		$array = false;
		parse_str('id='.$singleString,$array);
		$myArrays[] = $array;
	}
}
print_r($myArrays);

#
#	Option for that will work with either single or multiple
#
$myString = 'id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05
id=242810&type=test_event&signature=d64bb5ba162774f03005879b2954b1d44c2d56ee×tamp=2011-09-29%2018%3A26%3A05';
if(strrpos($myString,'id='))
{
	$myStrings = explode('id=',$myString);
	$myArrays = array();
	foreach($myStrings as $singleString)
	{
		if(trim($singleString)!='')
		{
			$array = false;
			parse_str('id='.$singleString,$array);
			$myArrays[] = $array;
		}
	}
	print_r($myArrays);
}
else
{
	parse_str($myString,$array);
	print_r($array);
}
?>


I cannot believe how well this works - thank you, thank you, thank you!
Was This Post Helpful? 0
  • +
  • -

#5 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Best way to capture strings in strings.

Posted 29 September 2011 - 07:20 PM

No problem :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1