tekfoo

Archive for 2010|Yearly archive page

PHP file handling

In PHP on May 6, 2010 at 4:06 pm


File handling functions are an essential part of any coding language. PHP has a collection of functions to read and to files too. We can use them to create, edit, delete, move, check the existence of and upload files. That’s pretty much everything you can do with files, except using them in a script helps you automate those tasks.

Prerequisites

Like always I assume that you know the basics of php. For instance if you can understand what this
$variable=”value”;
if($variable==”value”)echo “Variable has a value”;
means, we’re good to go.

How does php treat files?

There isn’t any huge difference between how we open and read files and how php does it. Every file has data, in simple words they’re all text, even the image and video files, only unreadable text. What do we do when we need to read or write into a file. We open it and do our thing. That’s just the way php does it too. No extra complications except for some code. You’ll understand it as soon as we do some coding.

Read files


Reading a file in php can be done using a number of function. We can group them on the ease of use. So there are easy and hard ways to open and read a file. Lets look at the easy functions first.

Easy ways or functions to read a file

Location of the file

Its important that you specify the right location of the file you want to read. So if the file’s in the same directory as the script the location should be the filename, but if its in the parent directory if the script the location would be ../filename. And if the file’s located in a sub directory of the folder that script is in location is subdirectory/filename.

1. file()

file() reads a file and copies it into an array on a line by line basis, each line is treated as one value of the array.
Here’s how you use it.
$text=file($fileName)

If the contents of the file would have been like this.
First line
Second line
Third line
The array would be the same as.
$text[1]=”First line”;
$text[2]=”Second line” and so on.

2. file_get_contents()

This one’s pretty much like file() too. The only difference is that it reads the contents of the file into a string. No arrays.
$text=file_get_contents($fileName);
If the contents in the text file are the same as in the first function, the value of the $text variable would be like.

$text= “First line \n Second line \n Third line \n”;

3. readfile()

This ones a very much different from the other two in the manner it deals with the output. It reads the file and displays it on the screen. For example.
readfile($fileName);
Would output “First line \n Second line \n Third line\n” on to the screen. You cannot store it in a variable.

The little hard way, my way

Remember I told you about php opening the file before reading or writing to it, well.

Meet the big guy fopen(), file opener function.

It does exactly what its name suggests, opens a file to read or write. Now you might be thinking, why open a file where as we can use the easy functions like read or file_get_contents. You’ll have the reason soon, lets see how far the rabbit hole goes.

Something about fopen

But before we continue, fopen unlike all the functions above, takes two arguments.
fopen($fileName, $mode);
The first one as we can see is the filename, but the second one seems a little new. Mode tells fopen the reason we’re opening the file for. See? fopen can be used to open a file for both reading and writing. So if we’re opening the file to read we should specify “r”, the first character in read as mode and if we’re opening it to write we specify “w” as mode. Isn’t that convenient?
Keep in mind that.
“r” is the mode to read
“w” is to write

Alright lets open up the file to read.
$text=fopen($fileName,”r”);
Go ahead, you should try this, chose any text file and open it using fopen().

Lets echo the output now.
echo $text;
Did you see something like this Resource id # some number? What happened there? Well don’t worry its supposed to happen. Using fopen alone doesn’t read a file, it just opens a file for reading. The variable that uses fopen turns into a file handle.

File handle

Lets think of file as a room or a file cabinet. What you we do to see what’s in the room or file cabinet? We open it using our hands :P . File handles are the hands that open the file. They open the file and keep them open till we close them. That makes sense. Doesn’t it?

So $fileHandle=fopen($fileName,”r”);

1. Use fread to read the fopen’ed file

Ok, so fread here’s the function we use to read the opened file. We’ll use fwrite if we need to write, but we’ll discuss it later. This is how we make it read the fopen’ed file. fread has two arguments too damn.

$text=fread($fileHandle, $howMuchWeWantToRead);

$fileHandle’s an important thing here. We must always provide an fopen’d file handle to fread as its first argument. Its our file opening hand :P . fread cannot read a file that’s not opened. The other weird argument $howMuchWeWantToRead like the name suggests read the amount of data we specify in bytes we need it to read. It provides more control over reading the file. If we want it to read the whole file we need to provide it with the full size of the file in bytes. But how to we do that.

filesize() to the rescue

filesize is a very useful function. It provides us with the size of a file in bytes. There are a lot of other uses too. This is how its used.

$size=filesize($fileName);

$size will have the size of the file in bytes. So till now our file reading code should look like this.

$fileHandle=fopen($fileName,”r”);
$size=filesize($fileName);
$text=fread($fileHandle,$size);

Try echo’ing $text now.
echo $text;
Aha, the file has been read and output ed.

Quite a lot of work to open and read a file you might think. But the easy functions do it too, it just does it automatically. Doing it like this helps us know how things work and provides us with more control.

2. fgets

Don’t worry, its the same as fread, use it in case you get bored of fread or need a shorter named function like it. But it has one difference. You don’t need to specify the size of how much you want it to read, it just reads a whole line or till it reaches the end of a line if you don’t specify the length of how much to read. This is how we use it.
$text=fgets($fileHandle,$length[optional]);

You can also use it like this if you don’t wanna specify the size of the file but need to read the whole file.

$text=”;
while(!feof($fileHandle)){
$line=fgets($fileHandle,100);
$text.=$line;
}
echo $text;
Yea, you must have seen the new function feof up there. Its a simple function that checks if the pointer has reached the end of the file. Ah whats a pointer then? Well lets talk about opening a book to read, in order to finish reading it we need to read it line by line till we reach the end. Pointer’s the thing than helps the fileHandle move from line to line. Still not clear? Pointer’s and fileHandles are to the script as our eyes and hands are to us :P . When each loop finishes and each fgets reads the pointer moves further just like us turning pages. I know it sounds weird. But it’ll make sense soon.

feof()

feof’s a function that check’s if the pointer has reached the end of the file. This is how we use it.
feof($fileHandle);

You can use the same method with fread too. Just replace the fgets with fread.

There are a few more functions too, but we’ll just stick to the basic ones for now.

Now that we’ve done some extensive reading on reading files with php lets do some more reading on writing files with php, yay :D .

Write to files

This section’s going to be pretty short since we’ve covered all the basics up there. And like before we have the easy and hard ways and functions to write to a file.

The easy ways

1. file_put_contents

file_put_contents just like file_get_contents with the exception that we use it to write to a file. It takes three arguments. Two necessary and one optional. Here’s how we use it.

file_put_contents($fileName,$data,$option);

It takes a filename and writes $data to it, if the file doesn’t exist it creates it, pretty easy to understand. But the issue is that it overwrites the data in the file if it already exists. That’s when the third argument comes into play. It helps us set optional flag if we don’t want it to overwrite the file. So if we don’t want it to overwrite the data in an already existing file we set FILE_APPEND as the third argument. In this case it just appends the data on top of the existing ones. For example. If the file has data “old data” in it the new data will be appended to it and the file will look like “old data \n new data” now.You can also use the flag or option LOCK_EX which locks the file so that no one else writes to it while we do.

The little hard ways

1. fwrite

fwrite works almost the same as fread, the only difference is that it writes instead of reading and like file_put_contents creates a new file if the file doesn’t exists and appends or overwrites the data in the existing file depending on the mode you specify. This is how we can use it.
fwrite($fileHandle,$data,$maximumAmountOfDataAllowedToBeWritten);
The third argument is optional and is like a safety feature so that we don’t write more data in case we have a limit to write to the file. But you won’t be using it mostly.

Some modes for fopen’ ing a file

‘w’ Open a file to write. Create it if it doesn’t exist.
‘r’ Open a file to read.

‘w+ Open a file to read and write, attempt to create it if it doesn’t exist
‘r+’ Open a file to read and write

‘a’ Open a file in the append mode or don’t overwrite mode :P . Create it if it doesn’t exist.

‘a+’ Open a file in read and append mode. Create if it doesn’t exist.

2. fputs

Same as fwrite.

So that covers the basics of reading and writing files in PHP. Hope it was of some use.

Oh and always fclose what you fopen


PHP may not close the file if it ends prematurely. So its important that we close the file using fclose function, you know just to be safe or it may remained opened :P .

fclose($fileHandle);

This is how we fclose a file. Its not necessary to do this, but its a good coding habit.

Other file handling functions

unlink($fileName)

Deletes the file.

chmod($fileName,$perm)

Changes the permissions on a file.

chown($fileName,$user)

Changes the ownership of a file to $user.

copy($originalFile, $copyFile)

Makes a copy of a file in the destination provided as the first argument to the destination provided in the second argument.

file_exists($pathToTheFile)

Checks if a file exists and returns true or false depending on its existence.

mkdir($dirName,$perm[optional])

Creates a directory of the name $dirName with permission $perm if its specified.

rmdir($dirName)

Removes or deletes an empty directory.If the directory isn’t empty we’ll have to delete all the files in it.

link($fileName,$shortCut)

Creates a link or shortcut to the $fileName in the location or file specified in $shortCut.

basename($fullPathToTheFile, $suffix[optional]):

Given the full path to a file, this function will return the basename or name of the file. Its not much of a use, but who know, we might need it some day.

$name=baseName(“/path/to/file.php”);
//Returns file.php

$name=basename(“path/to/file.php”,”.php”);
//Returns file

PHP IRC Bot using fsockopen

In PHP on May 5, 2010 at 10:02 am

What in the name of Zeus is fsockopen?

If you didn’t already know, fsockopen is a function in enables us to connect to remote hosts by opening sockets to them. Have you heard some Linux user say everything in Linux is a file? Apparently fsockopen treats sockets like files too. You know what this means don’t you, We can use it like fopen to read and write from those open socket using functions like fwrite and fread. I think you can think of some cool uses for it already :P .

Things you need to know

The only thing I assume you know to completely understand this article is the basics of PHP.

Alright, so how do I create an IRC Bot?

Have you seen those fancy IRC bots and wondered how you can make them? Maybe not. But if you have, you’re in luck, because that’s what we’re gonna do in this article. Build an IRC bot, and its easy too. Woohoo :D .

But first some basic IRC commands

NICK: Sets the nick or display name you want to use in an IRC server or channel.

USER: I think its the same as nick, I’m not sure because, IRC clients like mIRC often set this variable as username on local computer. You can use the same value for for nick and user.

JOIN: Join a channel.

PART: Leave a channel

MSG: Send message to a user.

PRIVMSG: Like MSG but you can also use to send messages to channels.

QUIT: Quit IRC all together.

These are the commands we’re going to be using here. You can find more here.

How it works

Like I said fsockopen’s pretty much like fopen. So all we have to do is open a connection to the IRC server using.

$nick=”Bot”
$host=”irc.dal.net”;
$port=6667;
$chan=”";
$fp=fsockopen($host,$port);

Now that the connection’s been opened we can send data to the server using fwrite or fputs. Its as easy as writing to files.

Heres how we login to the server.
fwrite($fp,”NICK $nick\r\n”);
fwrite($fp,”USER localuser $host erl: luser \r\n”);
fwrite($fp, “JOIN #chan \r\n”);
fwrite($fp,”PRIVMSG #chan Hi, I’m an IRC bot\r\n”);

Its important that each command we send ends with “\r\n” because it simulates the return or enter key press that we normally do on terminal, it helps the server know that we’re issuing a command. And in order to keep it running we’ll need to implement a while loop so that it doesn’t end till we want it to.

while($msg=fgets($fp,200)){

$msg=explode(“:ping”,$msg);

$msg[0]==’ping’?fwrite($fp,”PONG “.$msg[1].”\r\n”):”";

//This checks for any line with JOIN in them, gets the nick of the person who joined the chat or channel and greets them

preg_match(“/:(.*)\!.* JOIN :.*/Ui”,$msg,$nick)?fwrite($fp,”PRIVMSG #php hello “.$nick[1].”\r\n”):”";

echo $msg.”\n”;
}

Everything in the code above’s pretty straight forward except for the line that checks ping requests and preg_match thats checks for new people joining the channels and greets them. The IRC’s protocol requires the server to check if the user has timed out or gone offline so that it can close the connection, that’s the reason why it sends :ping requests all the time, if we don’t respond with pong and the message to it the connection gets close.

The whole code would look like this.

Running it from the terminal

Its better to run it from the terminal because its easy to close using the Ctrl+c or z buttons rather than closing the browser tab. In order to run it from the terminal do this.

cd /to/the/script’s/location
php scriptname.php

Teaching it more tricks

There are a lot of things you can make it do like trivia games, or output random quotes or jokes, display google, and stuff like that. But its outside the scope of this article. The functions you need to make it do all those things are fsockopen, cURL and regexp.

Create a simple drop down menu with CSS

In CSS on May 3, 2010 at 2:09 pm

Drop down menus aren’t very hard to make, especially with CSS. If take a look at how it works, all it does is show up when you hover the mouse over a button and disappear when you move the mouse away.
So it mustn’t be that hard to make one right? Correct. I bet anyone with basic knowledge in HTML and CSS can build it in under 10 minutes or you can have your money back, oh we don’t have any money involved here, too many tele ads I guess. When we’re done the finished product would look something like this :P .

Download.

We can do it using a few unordered lists and a maximum of six CSS rules. Don’t believe me, look at the code. This is what our markup code should be.
<ul id=”menu”>
<ul>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</li>
Menu 1</ul>
<ul>
<li>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</li>
Menu 2</ul>
<ul>
<li>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</li>
Menu 3</ul>
</li>
</ul>
See what happened up there? We have a main <ul> tag with id named menu, each sub <ul> tags are treated as the visible part of the menu and sub-sub
<ul> tags under the them are hidden, they’re the drop down part of the menu. But markup alone doesn’t do anything we need CSS to do its thing here.

Alright, so what kind of CSS voodoo do we use do make some element disappear? Aha, display: hidden or display: none . But we’re gonna be using display:none because the hidden property seems to function weird on some browsers. Now that we’ve managed to hide the “drop down” menu’s we need to find some way to make them re-appear once mouse is hovered over the main menu. That is when someone hovers the mouse over menu 1, 2 or 3, we need the ul with items 1, 2 and 3 to appear. Thats when :hover comes into play. Its the property of CSS that lets us do something when mouse is hovered over an element. Our drop down menu’s done. The CSS part of the menu looks like this.

*{padding:0px;margin:0px;}

#menu{float:left;padding:0px;}

#menu ul{background:#131313;color:#ffffff;text-align:center;
background:url(menub.jpg);border-left:1px solid #343434;float:left;
padding:3px;list-style:none;width:100px;}

#menu ul ul{display:none;position:absolute;background:#131313;
color:#ffffff;text-align:center;padding:0px;margin:-4px;border:none;
width:100px;padding:4px;}

#menu ul ul li{float:left;border:1px solid #343434;width:99px;
clear:both;}

#menu ul:hover ul{display:block;}
#menu ul ul li:hover{background:url(hovb.jpg);color:#131313;
border:1px solid #fdae16}

Told you, easy peasy . Are the 10 minutes over yet? Make it 20 if it is.

The ie6 fix


Sorry it took me this long to update this. Looks like ie6, Microsoft’s wrath on mankind doesn’t know how to handle the :hover property in CSS. One solution for this will be that everyone switch to another way cooler browser like firefox chrome or opera, nah just kidding :P , that’s easier said than done. So lets see what we can do with some javascript using onmouseover, onmouseout events and setAttribute function. I was trying to keep pure from any script, but

file: menu.js

Get google search results with PHP

In PHP on May 3, 2010 at 8:16 am


Google’s awesome :D . Who among us hasn’t used it? Unless you’re Bill Gates or you’ve never used the internet, chances are that you’ve used it at the least once. What if you could get the search results and do something with it? Wouldn’t that be cool? Maybe not. But still knowing it could be of some use one day who knows.

Enough talk more code

The things you need to know before we can move on are the basics of php, regexp and curl. So lets do it.

What makes it tick?

Its not very hard to get search results from google, you might already know it. The link to the google search page is http://www.google.com/search?

All we need to do to is add a few GET variables and we’re good to make a custom search. The variables are.

Some basic search GET variables.

1. q

The search term variable. For example if you want to search for banana hammock the url would look like http://www.google.com/search?q=banana hammock

2. start

For search results ranging to multiple pages, this variable can be used to browse through each of them for example if there are 20 search results and each page displays 10 results, the query to display the second page looks like. http://www.google.com/search?q=banana hammock&start=10

3. num

Number of results to be displayed per page. By default google displays 10 results per page, if you want to change it you can increase it by setting which ever number of results you want in the num variable. If you want 20 results the url could look like http://www.google.com/search?q=banana hammock&num=20

Some advanced GET variables

4. as_q

Advanced search term. Used to set search term in advanced search.

5. as_filetype

Sets the filetype you’re looking for. This could be helpful if you’re looking for any particular format of file like pdf or swf. For example if you want your script to search for pdf files the search query should look like this. http://www.google.com/search?q=banana hammock&as_filetype=pdf

6. as_sitesearch

This variable specifies if we want google to look for search term in the pages of any specific website. For instance if we want to search wordpress.com for a word “blog” the url would be http://www.google.com/search?q=blog&as_sitesearch=www.wordpress.com.

That covers some of the essential variables we’re going to need. No for the scripting part. If you already know cURL and regexp its going to be a piece of cake. Heres a simple script.

<?php
$term="php"; // the search term
$site="http://www.google.com/search"; //google search url
$filetype="pdf"; //file type we are looking for

$cH=curl_init();
curl_setopt($cH,CURLOPT_URL,"$site?q=$term&as_filetype=$filetype");
curl_setopt($cH,CURLOPT_RETURNTRANSFER,1);
curl_setopt($cH,CURLOPT_FOLLOWLOCATION,1);

$resultText=curl_exec($ch);

//Gets the element in which the results are stored
preg_match("/<p id=resultStats>(.*)<\/p>/Ui",$resultText,$resultText);
$resultText=$resultText[1];

//Gets the result count
preg_match("/Results <b>1<\/b> – <b>10<\/b> of about <b>(.*)<\/b>/Ui",$resultText,$resultCount);
$resultCount=$resultCount[1];

//Results are stored in element class "gl", we use preg_match_all to get all the elements with results
preg_match_all("/(.*)/Ui”,$resultText,$results);
$results=$results[1];

//output the results
echo “$resultCount results of $filtype found for $term”;
foreach($results as $result)echo $result;
?>

Make use of google suggestions

In case you’re wondering what google suggestion is, its the feature of google which provides us with suggestions when we type a search term. You must have seen it, it looks something like this.

There are a lot of uses for it if you need one. Imagination’s the way to go.

How do we use it?


Its really easy to use it tho. Google uses ajax to send requests to this url http://www.google.com/complete/search?. The GET variables you need to know are.

1. qu The search term
2. xml Set it as 1 if you want the output you get to be in xml format.
3. json 1 if you want the output to be in json format.

So that’s it, we’re all ready to utilize the full or at least quarter of the potential of google :) . Happy coding.

PHP and the RegExp beast

In PHP on May 2, 2010 at 12:37 pm

Once upon a time

When I was new to PHP I wanted to make a bbcode parser, so I started looking for ways to find a specific string and replace each occurrences of  it with appropriate strings or tags, for example replace the smiley symbol : ) with <img src=’Images/smile.jpg’>. That’s when I came across something called regular expression, the first look at it sent chills down my spine, I mean look at it.


<?php
$text="<a href="www.wordpress.com">Wordpress</a>
preg_match_all("<a href=['|\"](http:\/\/www\..*\.[a-z]{1,4})['|\"])>.*<\/a>/i",$text,$matches);
echo "Found matches";
foreach($matches[1] as $mach)echo $match."
";
?>

And they lived happily ever after. Now don’t worry if you don’t understand it, everyone doesn’t the first time, its really easy. In case you’re wondering what the above line does, it gets every  http link from the text in $text and stores it in the form of an array in $matches. I know I know, I should be starting from the basics here, sorry I got a little ahead here, just wanted to show you whats in store for you here, I’ll explain everything I know about regular expression now.

The beast


We’ve been hearing a lot of regular expressions or regexps as its easy to type. But wth is a regexp? Well think of it as  a “find a word” function, it looks for specific patterns or pattern of words matches  them and if you ask it to store the resulting matches to a variable, it does so. In even simpler words if you want to find out if there’s a  word “foo” present in “foobar”, it does just that for you. Its a very useful function of PHP or other languages like Perl, but we’ll focus on PHP for now.

What good is it?

regular_expressions

Well, there are a lot of applications and cool uses for regular expression. Some of them are.
1. Like I said before you can use it to parse smileys and bbcodes.
2. You can use it to find out the number of occurrence of a word or letter in a string or text file.
3. You can use it to process the results returned by your cURL google search script.
4. Get links and image urls from webpages that you scrape.
5. Find and replace words, use it as a word filterer.
6. Validate form data like email address or data of birth checking for a specified pattern.

Prerequisite

The only prerequisite to understand or make use of this article is that you should know that basics of PHP.

Now for some coding

Alright, enough talk, how in the name of everything holy do we use regexps and do some cool stuff with it? Well, to begin with we need to learn 3 functions.

1. preg_match:

It should be obvious that its the function used to match a “pattern” in a string. Now whats a pattern? In short if you look at the first example /<a href=.*(http:\/\/www\..*\..*)>.*<\/a>/i, this stuff is called a pattern.It tells preg_match what you want it to, match. You might be thinking wth do we need all those weird looking symbols and characters for. Well, you don’t really, all those symbols and stuff are for complex pattern matching. If you just want to match a word “foo” in a string “foobar”, your pattern could look like this.

"/foo/"

Delimiters

And what are those two unholy “/” symbols doing at both ends of our pattern? Those are called “delimiter”, they mark the beginning and end of our pattern, it doesn’t really need be the “/”, you can use any symbol you want as long as you use the same one to mark the beginning and end of the pattern. That brings us to our next question, why do we need to mark beginning and end of the pattern? We need them so that we can use something called “pattern modifiers”.

Pattern Modifiers

Yay more new terms :D . Modifiers are kind of advance features that helps us make advanced kind of searches on texts. Stuff like if you want to make the search case insensitive greedy or ungreedy, do extra analysis, ignore newlines and treat the whole string as one line. I know some of this may look strange to you but it’ll make sense sooner. Modifiers come immediately after the end delimiter like this “/pattern/i” i is the modifier here which makes the search case less, so Pattern and pattern looks the same to it. Some of the modifiers are.
i: This modifiers sets case less searching so that preg_match
does case insensitive matching.

s: If this modifier is set the “.” metacharacter matches all the characters. The “.” metacharacter, we’ll talk about
metacharacters next, matches everything excluding the “\n” character. Using this will make the dot character match it too.

x: This modifier makes the preg_match ignore white spaces unless its specified inside character class, I’ve been talking about a lot of terms that I’ven’t explained. Please humor me for a moment here. I’ll explain it all after this section.

U: Its also called the ungreedy modifier. The thing widh preg_match is that if you make a pattern like this /[b](.*)[/b]/ to match all the text between the bold bbcode, it’ll match everything in between the first occurrence of [b] and the last occurrence of [b]. So something like [b]bold text one[b] something else [b] bold text two[/b] after parsing would look like bold text one something else bold text two because it matched everything in between the first [b] and the sencond [/b]. So we put the U modifier so that it matches each [b] text separately.

Metacharacters

Did you notice all the “.[]{}\()*” characters? Well, turns out they’re called metacharacters. What they do is that they help us make our patterns even more specific. Lets say for example we want to check if a line ends with foo. We can do it using the “$” metacharacter which tells the function to match the word at the end of the string.

The most commonly used metacharacters are.
$- : This meta-character helps us specify characters in the end of pattern we want to match.
For example:
/end/ matches words like blend, will send, and end.

^-: The circumflex symbol sets the characters at the beginning of the pattern to be matched.
For example:
/start/ matches words like started, startle, and start the engine.

. : The dot character matches any character with exception to the “\n” or new line by default unless you set the m modifier.

\ : Is an escape character. We can use it to escape special characters like $,[,],{,},*,.,(,), and ^ in case we use them in our pattern. For example if we want to check if a text contains $ sign, we could do this.

See what happened up there? The $ character is escaped using a \. If you don’t this this the compiler will return an error.

We can also use it to escape the delimiter if we use it. Like this. /s\/ash/

Character Class

Just what we need, more terms :P . Character classes are like baskets or menus or lists in which we can specify a range of characters we want preg_match to match. For example if we want a [b] bb tag to match only letters and not numbers our pattern could look like this. /\[b\]([a-zA-Z]){1,}\[\/b\]/. Aha two new signs. Those two signs are called minimum and maximum quantifiers. The “-” character marks the range of characters for example “0-9″, or “a-b,a-z”.

Quantifiers

Holy cow, whats that? Nah, don’t worry its not as complex as its name sounds. There’s only two kind of quantifiers, minimum and maximum. They set the limit to how many characters we want the pattern to match in a characters class. Its used like this, {min quant, max quant} where min quant and max quant specifies the minimum and number of characters. For example.
{1,2} Means that we want to match a minimum of 1 and a maximum of two characters.
{1,} Means that we want the function to match any number of characters from 1 to any :P .

preg_match(“/[a-z]{1,}/”): Matches text like abc, cde, and thingislong
preg_match(“/[a-z]{1,2}[0-9]{1,}/”): Matches text like ab123,ab1, and jk123123123

Another thing about character classes is that the “^” symbol signifies the characters we want to exclude from the search or the characters that the matching pattern shouldn’t contain. For example.

preg_match(“/[a-z^0-9]/i”): Matches words like abce, and word and doesn’t match word like abc123.

The ^ meta-character outside the [ and ] characters of the character class is not the same as the one inside it, both of them have different meanings.

More quantifiers

Just when you thought you won’t hear from the quantifiers any more. There are two more quantifiers we need to know about. The first one is “*” or the 0 or more quantifier, it matches one or more characters specified in the character class or character that precedes it. And “+” or the 1 or more quantifier matches one or more characters. For example.

preg_match(“/\w{1,}.*/”): Matches w, wo1, wo% and pretty much any one word followed by 0 or more characters other than \n or newline.

preg_match(“/\W\w+/”): Matches one or more of any character.

SubPatterns

SubPatterns are like, well, sub patterns, suppose you want to match a smiley : D in a text “yay : D” so that you can parse it to replace it with appropriate image. If we do it using a pattern like this.
preg_match(“/.*: D/”,$text,$match)

It’ll return an array with the whole text, thats the reason we need sub patterns. The start and end of a sub pattern can be marked with “(” and “)” characters. Now if we do this.

preg_match(“/.*(: D)/”,$text,$match);
We’ll get an array with a collection of arrays in it which has the sub pattern or the “: D” we want. We can use this to replace it with appropriate image tag and source. In this case the matching sub pattern will be in $match[1] and if we add more sub patterns just increase the index number like $match[2], $match[3] and so on.

Alright now that we’ve covered the basics of syntax and preg_match lets move on to the other two functions. Woohoo, more fun :P .

preg_match_all:

preg_match_all’s a function almost like preg_match with exception for the fact that it matches all the words that matches the pattern in the string or text. The thing with preg_match is that it stops once it finds the first match, it can only be used to find a single match or to check if a text matches a particular pattern. If you wanna do some text collection, preg_match_all’s the function you’re looking for.
You can use it like this.
preg_match_all(“/pa[t]{2}ern/”,”text for the pattern to match”,$matchStoringVariable);

You’ll see some sample applications for this at the end of this article.

preg_replace:

Now for the last function, preg_replace. We can use it to replace strings that match a particular pattern. For example if we want to replace words with two p’s in it in a text with just one p we can do it like this.
preg_replace(“/[p]{2}/i”,”p”,$text);

Or we can replace an array of patterns with an array of replacements.
$smilies=array(“/:\)/,”: D”,”: p”,”: (“);
$replace=array(“images/smile.png”,”images/lau.png”,”images/ton.png”,”images/sa.png”);
preg_replace($smilies,$replace,$text);

Or if we want to get some matches from a string and replace some parts of the text along with the text we got from the match function we can do this.

$bbcode=array(“/\[b\](.*)\[\/b\]/Ui”,”\[i\](.*)\[\/i\]/Ui”);
$replace=array(“<strong>$1<\/strong>”,”<em>$1<\/em>”);
preg_replace($bbcode,$replace,$text);

You must have noticed the $1 up there. That’s the sub pattern match text we got from the preg_match function. If we want more sub patterns we just need to increase the number near the “$” sign like $2, $3 and so on. Its that easy.

Special characters

There are some special non-printable characters and characters than can be represented using a backslash, they can help you make your patterns more precise. Some of them are.

\a Alarm or the BEL character, its usage usually outputs a sounds when the script is run on terminal console.

\f Formfeed

\t Tab

\n Newline

\r Carriage return, similar to newline

\d Digits

\D Non decimal digits

\h and \v Horizontal and vertical whitespace

\H and \V Any character that is not a horizontal or vertical whitespace

\s Any whitespace character

\S Any character that is not a whitespace

\w Any word character.

\W Any nonword character.

\b Word boundary

\B Not a word boundary

\G First matching position in a pattern or string

\A Start of a string or line

\Z End of a line or string

The characters have not been sorted on the basis of anything. I just wrote it down :P .

And they regexp’ ed happily ever after

I think I’ve explained about everything or almost everything I know about regular expressions in PHP. If you’d like to, please take a look at the sample code. They’re not much, but I’ll add more as I make them.

Sample functions

Do XSS filtering

<?php
function XSSFilter($str){
$unsafe=array("//”,”/\”/”);
$safe=array(“<”,”>”,”"”);
$str=preg_replace($unsafe,$safe,$str);
return $str;
}
?>

Check if mail address is valid
Check if a field has only numbers or if a field has only letters

<?php
function onlyNum($str){
$ret=false;
if(!preg_match("/[^0-9]/"))$ret=true;
return $ret;
}

function onlyLet($str){
$ret=false;
if(!preg_match("/[^a-zA-Z]/"))$ret=true;
return $ret;
}

BBCode parser

function bbCode($str){
$bbcodes=array(“/\[b\](.*)\[\/b\]“); //Add more bbcodes here
$replace=array(“<strong>$1<\/strong>”); //Add the replacement texts or tags here
$str=preg_replace($bbcode,$replace,$str);
return $str;
}

?>

I’ll add more later :P . Sorry I couldn’t do it now.

PHP and cURL

In PHP on May 1, 2010 at 11:20 am

Hey there, this is my first post, so I decided to make it about something I use a lot, PHP and cURL, yay :P . And what are some of its uses Mr. Smarty Pants? you might be asking. There are a lotta uses for php and cURL, imagination’s the only thing limiting you. Scroll through the next few paragraphs if you already know some of the uses of cURL.

Some of the things you can use php and cURL for.

1.

Scraping websites and forums:

Supposed you’ve signed up on a forum and want to know if there’s a new post in it or someone has replied to your post. You can either do it the normal way by visiting the forum, logging in, and browsing to the page and check it manually or you can do it my way, write a script to do it for you.

2.

Downloading pictures and files:

You can use it to automate the downloads of large number of files.

3.

Getting and processing search engine results:

Instead of visiting google every time you want to search something, you can write a script to get the search results and display it on your website. Isn’t that awesome? Nah, maybe not.

Silly assumption.

Before we can move on, in order to understand or make full use of this article, you must know, or I assume that you know the basics of PHP. Silly me :P .

Ok ok, now how do we do it?

Its really simple. You’ve already covered the half distance if you know the basics of php, thats because we’re only gonna be needing 3 functions.

1.curl_init(): We use this function to create a new curl handle. If you’re already familiar with fopen, they both are almost the same. We can initiate or set a new cURL handle like this,

$curlHandle=curl_init(“http://www.website.com”);

Or

$curlHandle=curl_init();

. Now that our cURL handle has been opened, lets set some options.

Don’t worry, they’re just options like website url, cookie, request type, that is GET or POST, proxy address if you’re using any, user agent, and stuff like that. Its basically something that helps you emulate a browser.

2. curl_setopt(): This is the function that helps us set the options or various properties of a curl request. But we must  always have an initialized or curl_init() curl handler to set an option. There are a lot of curl options. All of them starts with CURLOPT_. Some of them are.

CURLOPT_URL: Sets the URL we want to crawl, or visit or do stuff with.

CURLOPT_RETURNTRANSFER: Specifies whether the returned data should be stored in a variable or displayed promptly. Set true or 1 if you want it to be saved in a variable and 0 if you want it to be displayed.

CURLOPT_USERAGENT: Defines the useragent string to be used in the request. In case you’re wondering wth user agent is. its the  string that specifies the browser, operating system and other details of a user.

CURLOPT_POST: Set it as 1 if you’re planning on making a post request. Most login and register forms use POST request.

CURLOPT_POSTFIELDS: Specifies the POST request string. It looks just like the GET request string. You just need to specify the name of the textfield and the value you want to send. For example if the markup of a login form looks like this,

<input type=’text’ name=’uname’>

<input type=’text’ name=’pass’>

Your POSTFIELD value should look like “uname=username&pass=password”. Its that simple.

CURLOPT_COOKIE: Defines the cookie string or the contents of the cookie to be sent to the server. If you have multiple cookies for example ,user=username and pass=password, separate each of them using a “;”, so our cookie string should look like. “user=username; pass=password”;

CURLOPT_REFERER: Use it to set the referrer value of the request.

You can find more options at http://www.php.net/manual/en/curl.constants.php

We can use it like this.

curl_setopt($curlHandler,CURLOPT_[option name here], [optionvalue);

If you want to set multiple values you can just use an array to store all the options and use curl_setopt_array to set them. For example.

$opts=array(CURLOPT_URL=>’http://website.com’,CURLOPT_RETURNTRANSFER=>1);

curl_setopt_array($curlHandle,$opts);

And

3. curl_exec(): This is a pretty straightforward function, it executes our cURL function and stores the returned data in a variable if CURLOPT_RETURNTRANSFER is set to 1 or true and a variable is provided. We can use it like this.

$returnedData=curl_exec($ch);

And do what is it that we want with the returned data. :D .

Well, that covers the basics of cURL in PHP, hope it was of some use to you. I’ll write another article on  using cURL to scrape websites soon.

Hello world!

In Uncategorized on May 1, 2010 at 9:51 am

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Follow

Get every new post delivered to your Inbox.