Howto Regular Expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kahwooi
Forum Commoner
Posts: 25
Joined: Fri Dec 10, 2004 12:28 am

Howto Regular Expression

Post by kahwooi »

I have a string: 'sessUserIdx|s:2:"11";sessUserName|s:6:"kelvin";sessUserFullName|s:6:"Kelvin";'

How do I use preg to retrieve the sessUserIdx value = 11, the problem is the sessUserIdx can be located in different location in the string.

Thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You may want to look at the database sessions thread linked from Useful Posts.. it has an extraction function specifically for this format.
kahwooi
Forum Commoner
Posts: 25
Joined: Fri Dec 10, 2004 12:28 am

Post by kahwooi »

With this info, I want to know number and who is online.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

feyd wrote:You may want to look at the database sessions thread linked from Useful Posts.. it has an extraction function specifically for this format.
also since that is a serialized array.. you may try loading (unserializing) the array and acces the sessUserIdx key directly...
kahwooi
Forum Commoner
Posts: 25
Joined: Fri Dec 10, 2004 12:28 am

Post by kahwooi »

n00b Saibot, good idea, I will try it out.
kahwooi
Forum Commoner
Posts: 25
Joined: Fri Dec 10, 2004 12:28 am

Post by kahwooi »

$field = 'sessUserIdx|s:2:"11"';
preg_match("/\"[^\"\/]+/", $field, $userIdxValue);

I get this result = "11, I have no idea how to retrieve only 11, please help, I'm weak at regular expression.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

kahwooi wrote:

Code: Select all

$field = 'sessUserIdx|s:2:"11"';
preg_match("/"[^"\/]+/", $field, $userIdxValue);
I get this result = "11, I have no idea how to retrieve only 11, please help, I'm weak at regular expression.
1. Use a different delimiter, one that is easy to read and wouldn't clash with commonly or specifically used characters in the regex. My personal faveourite is ~

Code: Select all

$field = 'sessUserIdx|s:2:"11"';
preg_match("~"([^"]+)~", $field, $userIdxValue);
var_dump($userIdxValue[1]);
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

this may be what you are looking for, expressed as a SimpleTest test:

Code: Select all

function testReplicateUnserialize() {
	$str = 'sessUserIdx|s:2:"11";sessUserName|s:6:"kelvin";sessUserFullName|s:6:"Kelvin";';
	$desired = array('sessUserIdx' => '11', 'sessUserName' => 'kelvin', 'sessUserFullName' => 'Kelvin');
	$regex = '/(?:^|;)([^|]+)\|s:\d+:"([^"]*)"/';
	if (preg_match_all($regex, $str, $match)) {
		$result = array_combine($match[1], $match[2]);
	}
	$this->assertEqual($desired, $result);
}
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Quick on-topic question...

Does anyone know why the session variables are not serialised using the standard "serialise" function? It seems strange to me that the session string can't be unserialised normally, and was just wondering what the reason might be.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would suspect it has to do with its previous incarnation as working with register_globals and not being an array.
Post Reply