Forum Moderators: coopster

Message Too Old, No Replies

Match after a period?

         

JAB Creations

3:11 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to figure out how to make a regex to match after a period. So the string '24a6g3y5g35635.z' would match 'z'.

$cookie = 'audio.0_backgroundimages._browserpatch.1';

$my_pattern = '/.$/';

list($audio, $backgroundimages, $browserpatch) = split($my_pattern, $cookie);
echo $audio;

The desired match in this instance would be '0'.

- John

cameraman

4:59 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember that a period in a regex is a special character. If you're looking for a period you need to escape it with a backslash.

dublinmike

11:44 am on May 25, 2008 (gmt 0)

10+ Year Member



Hi there,

Try:


$my_pattern = '/\.(.+)$/';

Murdoch

2:23 pm on May 27, 2008 (gmt 0)

10+ Year Member



Let me see if I've got this right...

Your $cookie is 'audio.0_backgroundimages._browserpatch.1' and you want to do a split with the period as your delimiter, and you have three list variables, but you have 4 split instances. So yes as mentioned above you would need to escape the period (I personally like to bracket it, just in case you want more than one delimiter), so I would go:

list($audio, $backgroundimages, $browserpatch) = split('[.]',$cookie);

So in your example:

$audio = 'audio'
$backgroundimages = '0_backgroundimages'
$browserpatch = '_browserpatch'

Is that what you wanted to do? And is this a static format for your strings? Because you could just do a second list function afterwards on $backgroundimages:

list($int,$str) = split('_',$backgroundimages');

That would give you your 0

Receptional Andy

2:47 pm on May 27, 2008 (gmt 0)



If you only want to match one character, I would use:

if (preg_match("/\.(.)/",$str,$match))
{
$result=$match[1];
}

Note that I'm not the best at regex though!

JAB Creations

3:09 pm on May 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't been able to keep up with posts of late due to some goofed XP slipstream installs...

I reference the "sets" in a way like CSS...

property.value
property.value_property.value_property.value_property.value

There are more like a dozen properties and values stored in the cookie. I just shortened it to avoid being obnoxious on the thread by creating a horizontal scrollbar. ;)

All I need the regex to do is get the value of a property. In this instance that value simply is '0' (for the property audio). The goal of course is to echo $audio at the end of all things said and done and have it echo '0', or if you change the 0 in the cookie to 11 then echoing $audio would echo '11'.

I use the dots and underscores because they don't turn in to %20, %35, etc symbols (they're not considered entities I presume so what are they correctly referenced as?)

...any way the . I decided to use to "connect" visually (as this is PHP) for concatenate (not to actually connect them, just a symbolization of sorts). The underscore separates each set.

This also works though it seems somewhat repetitive? But it works! So I'm still interested in the proper regex so I guess this would go a ways to demonstrate the desired effects...

<?php
$cookie3 = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie3);
foreach($pieces as $val) {
$contents=explode('.', $val);
$$contents[0]=$contents[1];
}
echo '$audio = <b>'.$audio."</b><br />\n";
echo '$backgroundimages = <b>'.$backgroundimages."</b><br />\n";
echo '$browserpatch = <b>'.$browserpatch."</b><br />\n";
echo '$chatroom = <b>'.$chatroom."</b><br />\n";
echo '$connection = <b>'.$connection."</b><br />\n";
echo '$css3 = <b>'.$css3."</b><br />\n";
echo '$cursors = <b>'.$cursors."</b><br />\n";
?>

Any way I'm still interested in the regex which I haven't been able to figure out yet. If this method above however creates less of a load I'd happily use it instead however. Either way the more ways I can figure out how to do any one particular thing would hopefully help me figure out how to change things to make them more efficient later on.

- John

Murdoch

4:16 pm on May 27, 2008 (gmt 0)

10+ Year Member



Looking at your example I don't know if I would use a regex but how's this?

$cookie3 = 'audio.0_backgroundimages._browserpatch.1_chatroom.0_connection.0_css3.0_cursors.0';
$pieces = explode('_', $cookie3);
foreach($pieces as $value) {
list($cat,$val) = split('\.',$value);
echo "$cat = <b>$val</b><br />\n";
}

That seems to work and use a little less code. Oh well best I could do from what I inferred you needed done.

-Doc