PHP regex match *after* a period?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP regex match *after* a period?

Post by JAB Creations »

How do I match after a period?

Code: Select all

$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
$my_pattern = '//';
list($audio, $backgroundimages, $browserpatch, $chatroom, $connection, $css3, $cursors, $dhtmleffects, $dtd, $ieccss, $keyboardlayout, $mediatype, $personality, $powerkeys, $sounds, $theme) = split($my_pattern, $cookie);
echo $audio;
With the correct regex echoing $audio will result as '0' and $initialfocus will result as 'content' in examples.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

Code: Select all

$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
   
$settings = array();
if (preg_match_all('/(?:(.+?)\.(.*?)?)_/', $cookie, $result))
    foreach ($result[1] as $ix => $key)
        $settings[$key] = $result[2][$ix];
 
 
var_dump($settings);
 
:drunk:
Last edited by VladSun on Sat May 24, 2008 10:32 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

And I would advice you not to use extract() - it will "polute" your namespace and could probably make your code vulnerable to variable value injection.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP regex match *after* a period?

Post by JAB Creations »

Yes I noticed the message at the bottom after the examples on php.net about extract.

Ok I should clarify that I only need the very base functionality of this...and then after I have the answer I'll be able to put it in to a foreach loop. If I try to do both at the same time my head will likely explode('chunks', $brains); :mrgreen:

So I took your regex string and applied it to my script (I like having regex merely assigned to a variable since I could reuse the regex (the variable) over and over)...

Code: Select all

$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
$my_pattern = '/(?:(.+?)\.(.*?))_/';
list($audio, $backgroundimages, $browserpatch, $chatroom, $connection, $css3, $cursors, $dhtmleffects, $dtd, $ieccss, $keyboardlayout, $mediatype, $personality, $powerkeys, $sounds, $theme) = split($my_pattern, $cookie);
echo $audio;
..and I get the error...
split() [function.split]: REG_BADRPT
So just the regex string is what I'm interested in please. I'm not finding anything useful on the error message itself but then again I'm not sure what you made the filter do exactly? None of the regex sites explain how to select something ***after*** a match which I find odd!

Once I get this working I'll share my newly revised PHP class file. I think it's come a long ways from all the procedural programming I had before. :)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP regex match *after* a period?

Post by JAB Creations »

I was thinking something like this maybe?

Code: Select all

$my_pattern = '/^\./';
...but I'm not sure how to say "not until after the character" in regex. :?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

It doesn't make sense to use such variable encoding when you know what order you are expecting ...
I mean, you have this:

Code: Select all

$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
one should suppose that you are pasring it in the way ("variable_name"."variable_value"_)
But your next code line:

Code: Select all

list($audio, $backgroundimages, $browserpatch, $chatroom, $connection, $css3, $cursors, $dhtmleffects, $dtd, $ieccss, $keyboardlayout, $mediatype, $personality, $powerkeys, $sounds, $theme)
implies that the variables are always ordered this way.
So ... if you are going to use it, then the encoding should be "audio-value"_"backgroundimages-value"_"browserpatch-value"...

What I intended to do in my solution was to get an associative array of values, regardless of how they are ordered in the string.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP regex match *after* a period?

Post by JAB Creations »

Actually this is one of the few places where I still prefer to do things statically. I know exactly what I set, if it's otherwise then it'll easily fail and thus I would presume less vulnerable to attempted attacks any way? That isn't my concern really. You're really good with very dynamic code though again I'm only taking baby steps towards my conversation from static voodoo to dynamic magic. :mrgreen:

Any way I've been reading more regex pages and I was thinking something along these lines...

Code: Select all

/^(\.){1}/
...but {1} is length, which won't be the equivalent of saying '1 character after' but instead it directly says 'one character in length'.

Gah! I'm so close I can smell victory!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

I don't think you could this with split() - because of the "var_name", "var_value" pairing you need to split twice. But if you use the '[._]' pattern you'll get an array of mixed varable names, values ...
I found an interesting function: array_combine() :)
So, I rwrote my code as follows:

Code: Select all

 
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
   
$settings = array();
if (preg_match_all('/(?:(.+?)\.(.*?)?)_/', $cookie, $result))
    $settings = array_combine($result[1], $result[2]);
 
 
var_dump($settings);
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

Have you ever looked at json functions in PHP?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP regex match *after* a period?

Post by JAB Creations »

*shrugs*

Honestly does it relate to the regex filter? Because it's all I need right now to get to point C. Weekends are almost a total drag to be stuck on something like this though at least you're trying to help me. The thing is not until I get to point C can I even begin to revise the route of B. I need those properties and values to be compatible with my existing system. I utilize classes and such just not to the extent you do. All the code has to do something but it has to also fit. I also can't afford hours and hours dealing with any one particular problem so as much as I am a perfectionist I aim my goals high enough so that when I implement something I make a reasonable amount of progress without throwing my naked body at a wall of spikes hoping random code will just work. So please for the love of sanity if you can show me how to do the regex I'll be more then happy to finish up my PHP class file and share it with you and everyone else to show you the progress I've made at my current level of ability.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

VladSun wrote:I don't think you could this with split() - because of the "var_name", "var_value" pairing you need to split twice. But if you use the '[._]' pattern you'll get an array of mixed varable names, values ...
I thought it was an answer.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP regex match *after* a period?

Post by JAB Creations »

Here is different though working way of doing the code that seems compatible with your style of doing coding...

Code: Select all

<?php
$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie);
 
foreach($pieces as $key=>$value) {
$value = explode('.', $value);
 
${$value[0]} = $value[1];
echo '$'.$value[0] .' = '. $value[1] .'<br>';
}
?>
You really should keep in mind that at my level of experience with PHP and with manipulating my PHP header class file I'm already really pushing my luck with things. I can not afford the luxury of experimenting with drastic changes on top of my already large number of planned changes that have not actually been tested and confirmed to actually work! If it wasn't such a critical part of my website I really would not mind messing around with newer stuff.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

My version of your code :)

Code: Select all

$cookie = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0_dhtmleffects._dtd.1_ieccss.1_initialfocus.content_keyboardlayout.designer_mediatype._personality.0_powerkeys.0_sounds.0_theme.classic';
 
$pairs = split('[._]', $cookie);
 
$settings = array();
for ($i = 0; $i < count($pairs) / 2; $i++)
    $settings[$pairs[$i*2]] = $pairs[$i*2 + 1];
 
var_dump($settings);
The advantage is that only one "explode" is performed.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP regex match *after* a period?

Post by VladSun »

And, hey - no hard feelings. You should know that if something goes harder and harder at every next step, then probably the very first step is somehow wrong ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP regex match *after* a period?

Post by JAB Creations »

I don't think you seem to be aware that it is critical to me to have the output as plain variables, not classes (not exactly sure what the output being generated though the dump doesn't show any plain variables such as $audio or $connection).

Any way I have something that works so I'm going to have to void potentially implement my attempt at a list split (which really irks me since it's so straight forward) without the necessary regex. It would have maximized adding something new and forward thinking at my level based as much on my own merit and with as minimal input from others as possible...my main goal to begin with.

Any way I just don't think you're grasping that I need out to be as variables.

So..............................................................

...since I have working code (albeit not the code that is most based on my own merit which dithers the spirit of my victory considerably) I'll get around to posting a little later my revised PHP header class setup once I have it all straightened out.
Post Reply