Regex to replace multiple spaces

Normally I just don’t worry about this since I’m usually building websites and HTML takes care of merging multiple spaces. But it comes up occasionally. And since it’s just a quick regex replace, it’s pretty quick to merge multiple spaces.

$x = preg_replace('/\s{2,}/g', ' ', "This     string   has     spaces");

Piece of cake.

It looks for any places that there is 2 or more spaces and replaces it with a single space.

Posted in Programming | Tagged , | Leave a comment

Hiding the “Welcome to your new WordPress site” message

Sitting here working on setting up a WordPress network and hit a snag. New sites have a giant “Welcome to your new WordPress site” message when someone logs in for the first time.

Now, I’m not against the message; and WordPress should definitely market where ever they can. But I’d like to be able to customize the layout a bit. Still plan on keeping links to WP and all of that, just want to add in some of my own welcome message.

Googling didn’t turn up anything except editing the core WordPress code which is a terrible idea. I don’t want to have to redo it every time WP upgrades. And it looks like there isn’t a hook or filter that can make it easy. So I had to make it ugly. Continue reading

Posted in Blogging, Programming | Tagged , , , , | Leave a comment

Weekly Tweets for 2012-05-12

Posted in Tweets | Leave a comment

Should add new comment be above existing comments

Had a thought while scrolling to the bottom of a WordPress post with 250+ comments. Does having the add new comment form at the bottom of a page that long lead to fewer comments? Really, who is going to scroll that far?

So I’m going to play around a bit here and move the comments form above existing comments. Guess we’ll see what happens.

And if you’ve tried the same thing and it worked, or didn’t, let me know.

Posted in WordPress | Tagged , , | 1 Comment

Datatables jQuery plugin – Refresh table

Sometimes I try and make things way too hard.

One of the things that a web app I’m working on needs is the ability to do an advanced search. It’s using the outstanding DataTables plugin for jQuery which takes care of most of the work. But the way I wanted to have the advanced search work is to repopulate the search field with a formatted string like “fieldOne:some info::fieldTwo:some more info” and so on for each of the 6 possible data elements.

That was pretty easy. Attached an onClick event handler to the search label to pop up a jQuery dialog which would then fill in the input field when one of the buttons was pressed.

Refreshing the table is where I hit a wall. Looking through the DataTables forum I found all kinds of posts on using fnDraw or a plugin called fnRefreshAjax that would supposedly work, but none did.

Then it clicked. DataTables was already doing what I needed to do when a keyup event fired. All I had to do was call the keyup event on the input field and it would take care of it for me.

A few hours later, and this little bit of code solved my issue.

$('div.dataTables_filter input').keyup();

One line, instead of about 20, and everything was working just like it should.

Posted in Programming | Tagged , , | Leave a comment

Double click on file in Chrome developer tools

Ajax file loaded in Google developer toolbox. A double click and it opens in the browser.

How did I not know this already?

I’m working on a web app that’s pretty Ajax heavy so I’ve got the Chrome developer tools window opened up to the Network tab so I can see what’s going on. By accident I double clicked on one of the files and it pulled it up in the main web browser.

Posted in Programming | Tagged , , | Leave a comment

Math for Game Programmers

Had an idea running through my head for a while now and finally pulled the trigger last weekend.

Something I’ve noticed teaching students to write games is that most don’t expect programming games to involve as much math as it does. Not that I’m teaching beginning programmers any math they haven’t already learned in a math class. And we’re talking 2-D games, so there’s nothing really ugly. But even doing things like xSpeed = xSpeed * -1 tends to throw a few off.

So last weekend I registered the domain name MathForGames.com and put up a WordPress site. As is pretty standard with brand new sites there’s not a lot there yet. But there are a few posts scheduled. By the time this post goes live there should be about 5 on Math for Games. Not impressive, but got to start somewhere, right?

Just finished a post on moving enemies through an array of way points in ActionScript if that sort of thing is interesting to you.

Posted in Blogging | Tagged , , | Leave a comment

PHP, __FILE__, and eval()

Came across an odd error last week. And by odd, I mean that Googling for the error message only turned up one page.

One of my scripts was using eval as a way to semi-protect the code. Yeah, not a great solution. But it’s not possible to install software on all of the users’ servers and PHP doesn’t have a way to compile. Hopefully that’ll come in the future though.

The error was /file.php (#): eval()’d code does not exist. Googling brought up a page that was having the same issue. After much head banging on desk it finally dawned on me to search my code for “does not exist” and that is what finally brought me to the solution.

What happened is that inside the code I was using __FILE__ to get the path to the eval()’d file and passing that path to another function. That function was checking to see if the file existed before including, and it was using the value from the __FILE__ value passed.

The problem is that __FILE__ inside of eval() includes information about the file being encoded, so “/file.php (#) eval()’d code” didn’t exist as a file on the server, and my function threw the not found error.

Took a bit of regex, and the problem was solved. Instead of passing __FILE__ I passed trim(preg_replace(‘/\(.*$/’, ”, __FILE__)) which stripped out the first parenthesis and everything following and then trimmed off any leading or trailing spaces. Problem solved!

Posted in Programming | Tagged | Leave a comment

Weekly Tweets for 2012-04-28

  • Tip from family this morning – If your house is spotless it means it's empty. #
  • Chocolate printer? Yes, please. http://t.co/fkvscwLX #
  • NASA State of Flux – See what humans have done to the planet http://t.co/av3l6aKu #
Posted in Tweets | Leave a comment

Roku remote for Android

Roku App Screenshot

Screen shot of the Roku Android App

A few months ago we bought a Roku for our bedroom, mostly for Netflix. Love it, except for the remote. It’s really easy to lose.

A quick search of the Android app store found a few 3rd party remotes that connect via Wi-Fi, but none seemed to work very well. There was always a bit of a delay between key presses, and none that I tried would connect to the Roku when it had powered itself off.

Last week found the official Roku Android app, and it’s much better than anything I’ve found. Picks up my Roku almost immediately instead of scanning the network every time. Button clicks are almost instant. The remote screen has buttons for the 3 most recently used channels. And, the best part, it works even when the Roku has gone to sleep.

Posted in Random | Tagged , | Leave a comment