2009
10.01

View file source

This is so simple it doesn’t require much of an explanation. User inputs file URL-> Script shows page source, similar to any web browser’s View Source function. What’s the use in this? I don’t know. I was using it for something completely different. I guess one could do some lightweight content scraping with it. Meh.. If you need something, it’ll come to you. Enjoy.

Mirrors

http://pastebin.com/f62d260a6
http://pastebin.ca/1588723
http://www.codeupload.com/127
http://slexy.org/view/s22Kw5bhSM
http://paste-it.net/public/ade8a82/

2009
09.28

PHP Stylesheet Switcher

With a bit of planning, you can make your site very dynamic, very easily. Using rich, definitive XHTML tags gives an extreme amount of versitility with CSS. What we’ll be doing here is using php to allow the user to pick which style they would like to see. Similar to the likes of most phpBB forums, or Last.fm.

To start, create two stylesheets. That’s not too difficult, is it? Name them black.css and red.css. After we do this, we’ll create a php file that sets a cookie, and displays the stylesheets. To make things easy to grasp, we’ll start out with an empty if() statement.

1
2
3
4
5
6
<?php
$style = $_GET['style'];
if(!isset($style)) {
 
}
?>

On the first line, we set a variable for $style, and since it’s a $_GET variable, you’ll need to take further action in securing it. And then we set the if() statement, that basically says if $style is NOT set. Now we’ll follow up on that and add in some code within the if statement.

1
2
3
4
5
6
7
8
9
<?php
$style = $_GET['style'];
if(!isset($style)) { 
	echo '[';
	echo '<a href="'.$PHP_SELF.'?style=black">Paint it Black</a> | ';
	echo '<a href="'.$PHP_SELF.'?style=red">See Red</a>';
	echo ']';
}
?>
output…

[Paint it Black | see Red]

So there we have our links ready. Take notice of how the parameters of ?style= are ‘black’, and ‘red’. That’s what we named our two stylesheets, remember? We need to keep that order. If they don’t match the filenames, we’ll run into trouble.

Now we’ll get to that cookie. Everyone likes cookies, yes? Of course. Include this follow snippet in with your last snippet:

1
2
3
4
5
6
<?php
if (file_exists('/css/'.$style.'.css')) {
	setcookie("styleswitch", $style, time()+(31*86400), '/');
	header("Location: setstyle.<?php");
} 
?>

Beautiful. First we check to see if the stylesheet is actually there, and if it is, we create a cookie named ’styleswitch’, collect which style the user picked, and set the cookie to stay fresh for a month (31*86400=31days=1month).

Next we’ll actually execute the switch. You know how you include your <link rel=”stylesheet” code in the head tags? We’re going to do that, just a little differently.

1
2
3
4
5
6
7
<?php
if(isset($_COOKIE['styleswitch']) && file_exists($_COOKIE['styleswitch'].".css")) {
	echo '<link rel="stylesheet" href="/styles/'.$_COOKIE['styleswitch'].'.css" />';
} else {
	echo '<link rel="stylesheet" href="/styles/default.css" />'';
}
?>

First we check to see if the cookie has been set, then we make sure once more that the stylesheet is a real file. If eitehr of those fail, we revert to the default stylesheet, ‘default.css.’

Aside from a bit of configuration and optimization, you’re done

Also…

Don’t stop here. Look for more info regarding cookies, and securing $_GETs. If you have any questions, feel free to post them.

EDIT: I just fixed a few things within the coding. WP-Syntax seems to be eating some things… Or I just don’t know how to properly use it.

2009
08.22

Fun with RegEx.

Well, it’s Saturday. Instead of sleeping, I’ll do something relatively useful. Which I’ve been failing miserably at doing as of late. Oh well. Enjoy.

Get rid of duplicate text

This can be useful for quie a few things. I used it to remove duplicate emails. You can even configure it to re-write the text file, if you do a bit of Googleing.

1
2
3
$text = file_get_contents('../info/emails.txt'); //grab the contents
$textMinusDups = preg_replace("/s(w+s)1/i", "$1", $text); //get rid of duplicates
echo($textMinusDups); //echo the rest

Highlight Text

This could be used for search results, most likely. But you can find something to suit your needs. Hell, with a little Ajax, you could make yo own CTRL+F function. Of course, I don’t condone using keystrokes on websites.

1
2
3
$words = 'Some say \'Jesus Saves\', but they don\'t know. One day their Magic Marker signs will say \'Mr. Evil pwns.\'';
$highlightFind = preg_replace("/b(Mr. Evil pwns.)b/i", '<span style="background:#ff0000;color:#fff;font-size:300px">1</span>', $words);
echo $highlightFind;

Grab All Images

I’m havin trouble thinking of any way this could be useful for images only, but someone can. Expandable to any file extension

1
2
3
4
5
6
7
8
9
10
11
12
$images = array();
preg_match_all('/(img|src)=("|\')[^"'>]+/i', $data, $media);
unset($data);
$data=preg_replace('/(img|src)("|'|="|=')(.*)/i',"$3",$media[0]);
foreach($data as $url){
	$info = pathinfo($url);
	if (isset($info['extension'])){
		if (($info['extension'] == 'jpg') || ($info['extension'] == 'jpeg') || ($info['extension'] == 'gif') || ($info['extension'] == 'png')){
			array_push($images, $url);
		}
	}
}

Grab Page Title

I would use this for bookmarking. You know, user inputs URL, script saves it, and grabs the page title. I digress. Have fun.

1
2
3
4
5
6
$grabPage = fopen("http://www.mrevil.feedthedragon.net","r");
while (!feof($grabPage)){ $page .= fgets($grabPage, 4096); }
 
$title = eregi("<title>(.*)</title>",$page,$regs);
echo $regs[1];
fclose($grabPage);

BBCode Smilies

This is wicked simple. And chances are, if you have a use for it, you already know how to do it. Oh well.

1
2
$text = '[:-)]';
echo str_replace('[:-)]','<img src="smilies/smile.png">',$text); // convert [:-)] to the image
Easy AdSense by Unreal