This blog is about anything that interests me. On the whole, as I'm a web developer, there will be a lot of webbie posts, but I may well stray off topic every now and again.

Friday, March 30, 2007

Using PHP and Regular Expressions to tidy up Variables

I recently had to develop a very simple email manager for a client. It was necessary to extract some text from a database and then to insert that text into an email message, which was then fired off to a mailing list.

The problem I had was that the text contained HTML tags as well as other HTML characters (for example, nbsp;), and I only wanted plain text in the email.

(Please note that for display reasons I've omitted the ampersand from the start of the non-breaking space.)

I was able to use the PHP strip_tags function to remove the HTML tags (see below), but this still left me with several HTML characters in the text.

The use of a regular expression solved the problem.

Here is the bit of code I used to clean up the contents of the variable:

// Get rid of HTML tags
$contents = strip_tags($contents);
// Get rid of non-breaking spaces
$pattern = '/nbsp;/';
$replacement = ' ';
$contents = preg_replace($pattern, $replacement, $contents);

When I extracted the piece of text from the database I placed it in a variable called $contents. I then ran the PHP strip_tags function on the variable to get rid of the HTML tags.

Next we have the bit of code that includes the regular expression.

$pattern contains the HTML character we want to search for. Here, $pattern contains nbsp;, which is the HTML character for a non-breaking space. I needed to get rid of this and replace it with a normal space because it looked a bit strange in the email message. For example, I needed to change:

'thisnbsp;week'snbsp;specialnbsp;offernbsp;is...'

to:

'this week's special offer is...'

$replacement contains a blank space, which is what I want to replace nbsp; with.

The last line in the bit of code is the actual regular expression.

For articles and photos to do with computer history, visit http://www.computernostalgia.net

Thursday, March 29, 2007

Linux v Windows

I've been working with computers since the mid-80s, initially using an Apple Macintosh and then moving to Microsoft Windows-based machines around 1991.

The first version of Windows I used was version 2.x (I can't remember the exact number), which I seem to remember didn't do anything too special. However, with the introduction of Windows 3.0, things started to improve dramatically, with Windows providing an environment in which you could 'comfortably' run applications.

Since then, like half the world I think, I've used various versions of Windows and I currently use XP. I have to say that, on the whole, I'm quite a fan of Windows. I've always found it fairly easy to use and it's always (well' almost always) enabled me to do my job without too many hickups.
A couple of years ago though, I bought a book on Linux which included a CD for Fedora Core 1.0. I wasn't sure what to expect when I installed the software, but what I got really impressed me. The user interface was nice and clean and, once you got used to it, fairly easy to use. OK, it wasn't as polished as Windows, but considering it was free, it was very good!

I couldn't get my printer to work, but I know that a lot of hardware manufacturers only provide driver software for Windows, so unless someone has written a specific Linux driver for your bit of hardware, it won't work. I think even then, some hardware just won't work with Linux. That's no big deal though, as there are loads of printers and so on that do work with Linux, so it's just a case of checking that a piece of hardware is supported before buying it.

What really surprised me though was how amazingly fast Internet access was. Opening web pages in Mozilla on Linux happened in an instant - as fast as if the page was on my hard disk! I have fast broadband access, but even so, I still have to wait sometimes while IE7 opens a page on Windows. Not long enough for it to be a problem, but there is a slight delay there. With Mozilla/Linux though it's instantaneous.

So, this brings me to me question - will Linux push Windows out of the frame? After all, the fact that it is free has got to be a big selling point (not that is has to be sold, of course). I understand that in developing markets (China, India, etc), Linux is very popular. These are massive markets, which will undoubtably influence the IT industry in lots of ways. The more people that use something, the more popular it becomes, which causes even more people to use it - the snowball effect.

I guess the question is, if I had a blank machine with nothing on it at all, would I install Linux or would I play it safe and install Windows? If it was a machine that only I was going to use then, yes, I think I would make the switch to Linux. However, if it was a machine that my wife and kids were going to use, I would probably stick with Windows. After all, my wife just wants the easiest solution available, and that's Windows.

The lack of hardware support is still the big sticking point, I think, to mass adoption of Linux. If this changes though, I think in a few years time we could see Linux becoming more mainstream as far as home and business use is concerned.

http://www.computernostalgia.net

Career Success Is 75 Percent Confidence And 25 Percent Ability

I'm a great people watcher and I've been observing other people's careers for the past 25 years.

OK, maybe I should have spent more time concentrating on my own career and then perhaps I would have been more successful at work myself. But, hey, I'm happy, so what the hell!

Based on this long and interesting period of observation, I feel confident in saying that the most important factor when it comes to being successful in the workplace is how much confidence you have. If you're the type of guy who wants to quietly sit in the corner working away, you'll probably never get anywhere no matter how good you are. If, on the other hand, you have tons of confidence, the amount of ability you have doesn't really matter too much - you'll probably be successful. Especially if you know who to direct that confidence at. There's no point in wasting too much time talking to other workers if they have no influence on your progression up the corporate ladder. No, your time will be much better spent only talking to influential people - the ones who will get you that ever important pay rise or promotion.

Obviously, if you have some ability as well, then so much the better, but ability is very much secondary - confidence is what matters. Also, never let lack of knowledge of the product get in the way - that doesn't really matter either. The key is to always offload any work on to other people and then, at the appropriate time, take all the credit for it. If it goes wrong of course you can simply blame the person you offloaded the work onto, and deny any involvement in it - perfect!

So, is it wrong to be so obsessed with success. No, not at all. If you regard your current job as being to get your next job - go for it. If on the other hand you take pride in what you do or produce at work, then that's fine as well. It takes all sorts.

Labels: ,