Strip/Remove IRC client control characters

Here’s a small PHP function that gets rid of the mIRC (or any other IRC client) control characters, that is the colors, underlines, italics, etc.

function stripControlCharacters($text) {
	$controlCodes = array(
		'/(\x03(?:\d{1,2}(?:,\d{1,2})?)?)/',	// Color code
		'/\x02/',								// Bold
		'/\x0F/',								// Escaped
		'/\x16/',								// Italic
		'/\x1F/'								// Underline
	);
	return preg_replace($controlCodes,'',$text);
}

PHP – Collapse multiple line drops to a single line drop

If you need to clean up some input by removing multiple new line drops and replace it with a single new line drop, for example, making this piece of text:

Lorem ipsum dolor sit amet,

consectetur adipiscing elit.
 

Aliquam ac elit at elit viverra mollis.

Cras tincidunt leo eleifend purus fermentum.

Look like this:

Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Aliquam ac elit at elit viverra mollis.
Cras tincidunt leo eleifend purus fermentum.

The following PHP function will remove the consecutive new line characters.

function collapseNewLines($str) {
	return  preg_replace('/((\r?)\n)+/', "\n", $str);
}

Windows/DOS new line is represented by the two characters \r\n and UNIX like systems use only the single char \n . The function handle both cases.
If you find it useful, a link to this blog would be nice 🙂

Logging telnet (Or anything else on a Linux/Unix shell)

I had some web server acting funny the other day so I decided to go Rambo style and get a page manually using telnet. I also wanted to save the output of the entire client/server negotiation for later analysis.
The Unix command script is just for that, it will run telnet (or any other shell command) for you and and allow you to work with it while capturing the standard input and output.

$ script -c "telnet www.aviran.org 80" output.txt
Script started, file is output.txt
Trying 208.74.149.35...
Connected to www.aviran.org.
Escape character is '^]'.

GET / HTTP/1.1
HOST: www.aviran.org
[Hit Enter Twice]
.
.  THE WEB SERVER OUTPUT THE REQUESTED WEBPAGE
.  INCLUDING HEADERS
.
Connection closed by foreign host.
Script done on Thu 15 Dec 2011 11:23:14 AM IST

$

After running script with the appropriate parameters, telnet connects to the server. Lines 7-9 are what I send to the server in order to fetch the root HTML page (You have to hit enter twice to end the input).
When telnet ends, the output.txt file will contain the entire input and output that was send to and from telnet.