Conquering Regular Expressions

So in between battling puppy pee, I’ve also been fighting with regular expressions for the better part of the evening. To summarize, I hate regular expressions. I just don’t get the logic, never have, and just when I think something makes sense, I’m told that I forgot like eight different escape characters or special circumstances or whatever and I feel like I’m back at square one.

With that said, after my last geeky code post of this nature, I decided that I want to get in the habit that when I fight with something long enough, I at least post about it online, both to vent and also to share in case someone else encounters something similar in the future!

Anyways, the scenario is this – working in PHP with WordPress, writing a custom category template for a new site I’ve been building. In this case, I needed to be able to pull the first image tag out of a post so that essentially I could create a category view that simply showed POST TITLE/DATE/ETC, IMAGE, MORE LINK TO VIEW ENTIRE POST.

Quickly, the caveats:

  1. I know that there are plug-ins to do this, however using code directly in the template is far more efficient because this will really only affect a fraction of the posts.
  2. Those plug-ins basically just parse out the tags, which won’t work for me anyways because I’m using NextGen Gallery for my images, meaning that the syntax stored in post is a shortcode like this – [singlepic id=123 w=550 h=413 float=center].

I fought with regular expressions for hours upon hours, but finally managed to come up with this:

(searching for this tag)
[singlepic id=123 w=550 h=413 float=center]

preg_match_all ('/(^\\[.*?\\])/is', get_the_content(), $matches);
$image = $matches[0]; $image = $image[0];
echo do_shortcode($image);

Basically it just looks for a tag in brackets at the very beginning of the post, which is my default format, however also meaning that it won’t get caught on anything if for whatever reason I end up using additional tags later on in the post. The first function does the actual search through the post content and throws the results into an array ($matches), with the second line extracting the tag out to a single variable and the final line executing it as a WordPress shortcode.

I’m still not entirely sure why I have to do line #2 twice – I guess between the preg_match_all() function and the data, it ends up as an array within an array, but after literally four hours of staring at this stupid thing, what you see above is officially the point where I said, “It works – good enough!”

Man eats 25,000 Big Macs, and I’M the overweight one?!

This just doesn’t seem fair. You may remember this guy from Super Size Me as the Big Mac fanatic who in his youth would eat 9 Big Macs a day! Of course, now that he’s older, he reserves himself to merely 1-2 Big Macs a Day…

http://www.youtube.com/watch?v=ZIZvnIYDS7k

For those of you playing along at home, 9 Big Macs a Day contain:

  • 4,860 calories (243% daily value – 2,000 calories = normal)
  • 261 grams of fat (408% daily value – 64g = normal)
  • 9,360 milligrams of sodium (387% daily value – 2,418mg = normal)

And yet here I am, eating a staggering ZERO BIG MACS A DAY, and yet between the two of us, I’m the one with the bulbous gut?!

Stupid metabolism, stupid genetics…

Doggy Discipline – We’re Doing It Wrong…

So as any former or current puppy owner might expect, we’ve spent the last four days trying to figure out how to teach our new puppy not to pee in the house. Heck, we’ve got those special puppy pads, so we’d even be happy with on the pads, not on the carpet, but after the short-lived novelty of actually using them for their intended purpose wore off, now all she wants to do is tear the things to pieces.

One approach that we had been trying despite it failing miserably was that for about a day and a half, if she peed inside, she had to spend five minutes outside on our patio … alone. She DID NOT like this, and so as a result, it basically led to her yelping and crying at the door like she was being murdered for the entire duration of each five minute period.

Not fun for any of us involved, or probably our neighbors either, for that matter!

Thankfully, Sara was doing some research this evening and discovered that in no uncertain terms, that’s a horrible way to teach a dog how to do anything! All the “punishment” was doing was upsetting her (and us), and in fact, we noted that since invoking this inane punishment, she had actually started peeing in the house more, in addition to being more clingy to Sara as well. Instead, ironically she read that our first attempt had actually been the correct one – to reward her and praise her when she goes outside, and to do our best not to cause a ruckus when she doesn’t.

Apparently you can’t scare a dog into not peeing on your carpet, but rewarding it with yummy treats when it pees in the preferred area – that’s the ticket.

At least, we’ll see. I feel like we’ve already gone through nearly half the bottle of carpet sanitizer already! Probably shouldn’t count on ever seeing that pet deposit that we just put down ever again…

The Story of Cleo…

So as you may have eluded from yesterday’s late night, adorable picture, we got a puppy.

She’s a beagle, barely 8 weeks old, adopted from someone who was fostering a pregnant momma beagle that came to Florida from Alabama. It’s a possibility that the father is a pointer, but no way to know for sure … we were just told that because of this, she could be upwards of 40-50 lbs, whereas beagles normally clock out in the low-20s.

Just between you and me, I really hope that she doesn’t get upwards of 50 lbs!

Anyways, her name comes from the first time we met her a couple of weeks ago – seeing how she liked to wait until all five of her brothers and sisters were asleep in a big pile, then climb her way to the top to claim her own bed, we came up with Cleopatra, Queen of the Pile, which even in her new home she’s already been exploiting more than she probably knows by giving Sara and I a constant run for our money! Sara put her in her cage last night to give her a chance to clean up some poop on the floor and I think Cleo lasted maybe 37 seconds of whining before yours truly came running in to spring her from puppy jail…

She’s been a lot of fun, though. Already we’ve learned that she’s afraid of beach balls, she doesn’t really care for her leash, although so far she’ll stick close even without it, and she’s curious about the pool … from behind the security of the safety fence, anyways! She’s very much a Momma’s girl (simply because Sara has spent the most time with her), and two days in, she’s yet to actually sleep in her own bed simply because we thought the only way we’d be able to get any of our own would be to just let her crash with us!

Of course, I’m being rapidly reminded of just how big of a puppy pushover I am, and also of just how much of this whole puppy raising thing that I lucked out of when Mom and Lori were raising Brandi! I honestly don’t really remember ever cleaning up after her or putting food out for her – aside from her occasionally peeing on my friends when they came over, that dog and I had a pretty good relationship. I am looking forward to her getting a little older so we can actually take her on walks outside of our own backyard, and the whole not peeing in my office or stealing my sandals will be nice, too, but I suppose that only comes one day at a time…

Meet Cleo!!!


She’s still a little camera shy, and everything shy, for that matter, but we’re gonna work on paparazzi training more this weekend…

Thin Post : Battling a Sleep Deficit

I know that I don’t get enough sleep.

And it’s irregular, too – some days I’ll sleep 1:00am – 7:30am, others I might pull a late night writing and be up until 3 or 4am. I end up paying for it later because if I don’t just collapse the next day entirely, I’ll find myself dragging over the following couple of evenings, rarely getting anything done in the process, which typically makes whatever progress I gained the first night moot.

I also wonder if somehow it also has an impact on my weight loss efforts.

I’ve done some reading and found various articles linking irregular sleep to weight gain, citing that it messes with your metabolism and stuff, and even though I don’t really understand much about it, it would kind of make sense if bedtime is when your body is generally doing upkeep and taking care of itself. Maybe it’s doing some other stuff with regards to how I store fat that I’m disrupting when I get 5 hours of sleep one night and then 10 the next.

Either way, it’s something that I want to work on because I have a feeling it might help, and it should also make writing easier if I don’t lose random days here or there to being sleepy. It’s gonna be tough because I’m very much so a night person – my ideal workday would probably be something like 11am – 3am if I had the choice! Maybe someday, but in the meantime I’ve made enough sacrifices for this effort, what’s one more that might also help me to be a little bit more productive to boot?

Rapture Watch 2011

I don’t really keep up on my religious nonsense these days, but I’ll admit that I got a good chuckle when I came across this company in the paper today who’s made a business out of offering to care for believers’ pets after the rapture takes them to heaven. That’s right, for only $135, you can rest assured that when you’re up there chillin’ with the Lord and whatnot, these folks will see fit that your beloved pets are taken care of … you know, until the entire world bursts into flames and life ceases to exist, of course!

The best part? As ridiculous as this sounds, they’ve already gotten 250 people to sign up for their service, which at $135+ a piece is a cool $33,750.

I mean, even if this rapture of theirs does come, who’s going to follow up on them to make sure that somebody actually goes to retrieve and take care of Fluffy and Buttercup in your divine absence?! Even if they do have binoculars or something in heaven to look back down, what are they going to do – call Eternal Earth-Bound Pets Customer Service to complain???

One other thing about this rapture thingy that I just did the math on – so there are supposed to be 144,000 believers who are “saved” in this event, right?

There are currently about 6.92 BILLION people on the planet.

That means that a whopping 0.000021% of the Earth’s population is set to be saved, which begs the question … other than that obnoxious, born-again guy from the bus stop not being there anymore, are the rest of us really even going to notice that these folks are gone?! That’s like losing the city of Bridgeport, CT (where?), or a little less than half the people who die from obesity in the US each year, or about 7.5 people from every single town in America.

Other than maybe having a couple of extra seats available at the bus stop or maybe an extra lane or two at the bowling alley, I guess I’m just not seeing what the real benefit of this whole thing is for the rest of us!