Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, January 30, 2009

Using Regular Expressions with PHP

Using Regular Expressions with PHP

Regular expressions are a powerful tool for examining and modifying text. Regular expressions themselves, with a general pattern notation almost like a mini programming language, allow you to describe and parse text. They enable you to search for patterns within a string, extracting matches flexibly and precisely. However, you should note that because regular expressions are more powerful, they are also slower than the more basic string functions. You should only use regular expressions if you have a particular need.

This tutorial gives a brief overview of basic regular expression syntax and then considers the functions that PHP provides for working with regular expressions.

* The Basics
* Matching Patterns
* Replacing Patterns
* Array Processing

PHP supports two different types of regular expressions: POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX ones, and faster too, so we will concentrate on them.

The Basics

In a regular expression, most characters match only themselves. For instance, if you search for the regular expression "foo" in the string "John plays football," you get a match because "foo" occurs in that string. Some characters have special meanings in regular expressions. For instance, a dollar sign ($) is used to match strings that end with the given pattern. Similarly, a caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string. The characters that match themselves are called literals. The characters that have special meanings are called metacharacters.

The dot (.) metacharacter matches any single character except newline (\). So, the pattern h.t matches hat, hothit, hut, h7t, etc. The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR operator and you should use it if you want to construct a pattern that matches more than one set of characters. For instance, the pattern Utah|Idaho|Nevada matches strings that contain "Utah" or "Idaho" or "Nevada". Parentheses give us a way to group sequences. For example, (Nant|b)ucket matches "Nantucket" or "bucket". Using parentheses to group together characters for alternation is called grouping.

If you want to match a literal metacharacter in a pattern, you have to escape it with a backslash.

To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9].

The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression", and ? means "Match zero or one of the preceding expression". Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times".

Now, have a look at the examples:

Regular Expression Will match...
foo The string "foo"
^foo "foo" at the start of a string
foo$ "foo" at the end of a string
^foo$ "foo" when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character that is not a uppercase letter
(gif|jpg) Matches either "gif" or "jpeg"
[a-z]+ One or more lowercase letters
[0-9\.\-] Аny number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers

Perl-Compatible Regular Expressions emulate the Perl syntax for patterns, which means that each pattern must be enclosed in a pair of delimiters. Usually, the slash (/) character is used. For instance, /pattern/.

The PCRE functions can be divided in several classes: matching, replacing, splitting and filtering.

Back to top

Matching Patterns

The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag argument and an offset parameter that can be used to specify the alternate place from which to start the search:
preg_match ( pattern, subject [, matches [, flags [, offset]]])

The preg_match() function returns 1 if a match is found and 0 otherwise. Let's search the string "Hello World!" for the letters "ll":
if (preg_match("/ell/", "Hello World!", $matches)) {
echo "Match was found
";
echo $matches[0];
}
?>

The letters "ll" exist in "Hello", so preg_match() returns 1 and the first element of the $matches variable is filled with the string that matched the pattern. The regular expression in the next example is looking for the letters "ell", but looking for them with following characters:
if (preg_match("/ll.*/", "The History of Halloween", $matches)) {
echo "Match was found
";
echo $matches[0];
}
?>

Now let's consider more complicated example. The most popular use of regular expressions is validation. The example below checks if the password is "strong", i.e. the password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit:
$password = "Fyfjk34sdfjfsjq7";

if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
echo "Your passwords is strong.";
} else {
echo "Your password is weak.";
}
?>

The ^ and $ are looking for something at the start and the end of the string. The ".*" combination is used at both the start and the end. As mentioned above, the .(dot) metacharacter means any alphanumeric character, and * metacharacter means "zero or more". Between are groupings in parentheses. The "?=" combination means "the next text must be like this". This construct doesn't capture the text. In this example, instead of specifying the order that things should appear, it's saying that it must appear but we're not worried about the order.

The first grouping is (?=.*{8,}). This checks if there are at least 8 characters in the string. The next grouping (?=.*[0-9]) means "any alphanumeric character can happen zero or more times, then any digit can happen". So this checks if there is at least one number in the string. But since the string isn't captured, that one digit can appear anywhere in the string. The next groupings (?=.*[a-z]) and (?=.*[A-Z]) are looking for the lower case and upper case letter accordingly anywhere in the string.

Finally, we will consider regular expression that validates an email address:
$email = firstname.lastname@aaa.bbb.com;
$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";

if (preg_match($regexp, $email)) {
echo "Email address is valid.";
} else {
echo "Email address is not valid.";
}
?>

This regular expression checks for the number at the beginning and also checks for multiple periods in the user name and domain name in the email address. Let's try to investigate this regular expression yourself.

For the speed reasons, the preg_match() function matches only the first pattern it finds in a string. This means it is very quick to check whether a pattern exists in a string. An alternative function, preg_match_all(), matches a pattern against a string as many times as the pattern allows, and returns the number of times it matched.

Back to top

Replacing Patterns

In the above examples, we have searched for patterns in a string, leaving the search string untouched. The preg_replace() function looks for substrings that match a pattern and then replaces them with new text. preg_replace() takes three basic parameters and an additional one. These parameters are, in order, a regular expression, the text with which to replace a found pattern, the string to modify, and the last optional argument which specifies how many matches will be replaced.
preg_replace( pattern, replacement, subject [, limit ])

The function returns the changed string if a match was found or an unchanged copy of the original string otherwise. In the following example we search for the copyright phrase and replace the year with the current.
echo preg_replace("/([Cc]opyright) 200(3|4|5|6)/", "$1 2007", "Copyright 2005");
?>

In the above example we use back references in the replacement string. Back references make it possible for you to use part of a matched pattern in the replacement string. To use this feature, you should use parentheses to wrap any elements of your regular expression that you might want to use. You can refer to the text matched by subpattern with a dollar sign ($) and the number of the subpattern. For instance, if you are using subpatterns, $0 is set to the whole match, then $1, $2, and so on are set to the individual matches for each subpattern.

In the following example we will change the date format from "yyyy-mm-dd" to "mm/dd/yyy":
echo preg_replace("/(\d+)-(\d+)-(\d+)/", "$2/$3/$1", "2007-01-25");
?>

We also can pass an array of strings as subject to make the substitution on all of them. To perform multiple substitutions on the same string or array of strings with one call to preg_replace(), we should pass arrays of patterns and replacements. Have a look at the example:
$search = array ( "/(\w{6}\s\(w{2})\s(\w+)/e",
"/(\d{4})-(\d{2})-(\d{2})\s(\d{2}:\d{2}:\d{2})/");

$replace = array ('"$1 ".strtoupper("$2")',
"$3/$2/$1 $4");

$string = "Posted by John | 2007-02-15 02:43:41";

echo preg_replace($search, $replace, $string);?>

In the above example we use the other interesting functionality - you can say to PHP that the match text should be executed as PHP code once the replacement has taken place. Since we have appended an "e" to the end of the regular expression, PHP will execute the replacement it makes. That is, it will take strtoupper(name) and replace it with the result of the strtoupper() function, which is NAME.

Back to top

Array Processing

PHP's preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it's necessary to split a string with a dynamic expression rather than a fixed one, this function comes to the rescue. The basic idea is the same as preg_match_all() except that, instead of returning matched pieces of the subject string, it returns an array of pieces that didn't match the specified pattern. The following example uses a regular expression to split the string by any number of commas or space characters:
$keywords = preg_split("/[\s,]+/", "php, regular expressions");
print_r( $keywords );
?>

Another useful PHP function is the preg_grep() function which returns those elements of an array that match a given pattern. This function traverses the input array, testing all elements against the supplied pattern. If a match is found, the matching element is returned as part of the array containing all matches. The following example searches through an array and all the names starting with letters A-J:
$names = array('Andrew','John','Peter','Nastin','Bill');
$output = preg_grep('/^[a-m]/i', $names);
print_r( $output );
?>

Monday, June 2, 2008

ASP.NET Interview Questions


*What is View State?
*Can you read the View State?
*What is the difference between encoding and encryption? Which is easy to break?
*Can we disable the view state application wide?
*can we disable it on page wide?
*can we disable it for a control?
*What is provider Model?
*Any idea of Data Access Component provided by Microsoft?
*Any idea of Enterprise library?
*What is web service?
*What is WSDL?
*Can a web service be only developed in asp.ent?
*can we use multiple web services from a single application?
*can we call a web service asynchronously?
*Can a web service be used from a windows application?
*What do we need to deploy a web service?
*What is the significance of web.config?
*Can we have multiple web.config files in a sigle web project?
*Can we have more then one configuration file?
*Type of Authentications?
*Can we have multiple assemblies in a single web project?
*What is GAC?
*What is machine.config?
*What different types of session state Management we have in asp.net?
*What are cookies?
*What is Cache?
*What is AJAX?
*Is AJAX a language?
*What is the difference between syncronus and asyncronus?
*What is an Assembly?
*Can an assembly contains more then one classes?
*What is strong name?
*What is the difference b/w client and server side?
*What we need for the deployment of a asp.net we application?
*what is the purpose of IIS?
*Difference between http and https?
*what is purpose of aspnet_wp.exe ?
*what is an ISAPI filter?
*what do you mean by HTTP Handler?
*What is the purpose of Global.asax?
*What is the significance of Application_Start/Session_Start/Application_Error?
*What is the difference between the inline and code behind?
*what is side by side execution?
*can we have two different versions of dot net frameworks running on the same machine?
*What is CLR? Difference b/w CLR and JVM?
*What is CLI?
*What is CTS?
*What is .resx file meant for?
*Any idea of aspnet_regiis?
*Any idea of ASP NET State Service?
*Crystal report is only used for read only data and reporting purposes?
*We can add a crystal report in aspx page using two techniques, what are these?
*What is the difference between stroed procedure and stored function in SQL?
*Can we have an updateable view in SQL?
*What is connection pooling? how can we acheive that in asp.net?
*What is DataSet?
*What is the difference between typed and untyped dataset?
*What is the difference bewteen accessing the data throgh the dataset and datareader?

1. What is the difference between encoding and encryption? Which is easy to break? Can we disable the view state application wide? can we disable it on page wide? can we disable it for a control?


Ans: encoding is easy to break, we can disable it page wide and control level.

2. Can you read the View State? Disadvantage of a view state. ( asked to me in IFLEX Solutions)

Ans: Yes we can read the view state since it is encoded using base64 string, so it is easy to break it, we also have view state decoders ( http://www.pluralsight.com/toolcontent/ViewStateDecoder21.zip)

3. What is provider Model? (asked to me in mindlogicx , salsoft)

Ans: Provider model means that microsoft has provided a set of interfaces, a new vendor just needs to implement them and write the functionality for their software to work with MS. example SQL provider model, ODBC provider model, MySQL proviuder model.

4. Any idea of Enterprise library?



http://www.dotnetjunkies.com/Article/29EF3A4F-A0C2-4BB2-A215-8F87F100A9F9.dcik



5. Can we have multiple web.config files in a sigle web project? ( asked to me in IFLEX Solutions, salsoft)



Ans: Yes we can have them for each sub folders in the web project main folder.



6. Can we have more then one configuration file?



Ans: yes



7. What is the difference between syncronus and asyncronus?



Ans: syncronus means you are waiting for the responce to come back, while in the asyncronus you start with the next staement and whenever the results come back a call back function is called.



8. Difference between http and https? what is purpose of aspnet_wp.exe ? what is an ISAPI filter? what do you mean by HTTP Handler?



Ans: https means it is using SSL, aspnet_wp is the worker process that handles all the asp net requests, ISAPI filter helps the iis in identifying the request type and forwarding it to the appropriate handler.



9. what is side by side execution?



Ans: It means that we can have two dot net version running on the same server machine, and the application build in asp.net 1.1 will be server by dot net framework 1.1 and the applications build in asp.net 2.0 will be server by dot net framework 2.0.



10. can we have two different versions of dot net frameworks running on the same machine?

Ans: yes



11. Can we have an updateable view in SQL?

Ans: Yes

12. What is the difference between typed and untyped dataset?

Ans: typed data set contains the inforation of the db/table structure

13. We can add a crystal report in aspx page using two techniques, what are these?

Ans:
1> embaeded in assembly
2> outside the assembly


What is View State?
view state is used to store the data in the controls on the page during postback
Can you read the View State?
yes we can read the view sate,
What is the difference between encoding and encryption? Which is easy to break?
encoding is form of transfering data over the network , encrytion is one form of encoding..

encoding is easy to break
Can we disable the view state application wide?
yes we can as follows
pages enableViewState="false"
can we disable it on page wide?
page enableviewstate="false"
can we disable it for a control?
TextBox id="txtCode" runat="server” EnableViewState="false"
What is provider Model?
provider Model is a interface designed by microsoft for different providers such as oracle,

access etc.. which need to be implemented in thier products for .net applciations to access

thier products..
Any idea of Data Access Component provided by Microsoft?
Data Access component is a datalayer designed by microsoft which is based on design pattersn

and an effective way of extracting data from the database, it a genric way of accessing any

database.
Any idea of Enterprise library?
Enterprise library is a collection of components designed by microsoft for the devolopers,

its code in dotnet languages for effective use in applications, it consists of effective way

of writing different layers of the application.
What is web service?
A web service is a collection of protocols and standards used for exchanging data between

applications or systems. Software applications written in various programming languages and

running on various platforms can use web services to exchange data over computer networks

like the Internet in a manner similar to inter-process communication on a single computer

What is WSDL?
WSDL (Web Services Description Language) is an XML-based language for describing Web

services and how to access them
WSDL or Web Services Description Language is an XML format for describing network services

as a set of endpoints operating on messages containing either document-oriented or

procedure-oriented information

Can a web service be only developed in asp.ent?
no
can we use multiple web services from a single application?
yes
can we call a web service asynchronously?
yes
Can a web service be used from a windows application?
yes
What do we need to deploy a web service?
we need to create a virtual directory in the iis, copy the asmx page, .disco file, config

file, and in hte bin directory place the dll.
What is the significance of web.config?
it contains the application specific configuration settings.
Can we have multiple web.config files in a sigle web project?
yes we can have , each in each directory of the application
Can we have more then one configuration file?
yes
Type of Authentications?
Forms Authentication
Windows Authentication
passport authentication
Can we have multiple assemblies in a single web project?
yes we can
What is GAC?
Gloabal Assembly cahce which contain assemblies shared by multiple applications
What is machine.config?
its configuration file which contain cofiguration settings common to all applications in a

system
What different types of session state Management we have in asp.net?
inproc and outproc, inproc is where the your webapplication itself takes care of storing the

state where this is useless in case of webfarm applications,

outproc is where u store the state in sql server external to your application, in the

web.config you have add the following tag to enable outproc

sessionState mode="SQLServer" stateConnectionString="tcpip=127.0.0.1:42424" _

sqlConnectionString="data source=127.0.0.1; integrated security=true" _ cookieless="false"

_ timeout="20"


What are cookies?
cookies are used one of the ways of storing the state of the user, the data of the user is

moved to and fro between the client and server, the cookies are generated by the server and

stored at the client end. and with each and every request the cookie is send to the server.
What is Cache?
cache is place of storage at the server end, wherein data can be stored at the server, cache

can be available at the page level or at the control level
diffrent cache mechanisam available are
Declarative Output caching - This is similar to declarative output caching in ASP.NET 1.x,

wherein you configure caching by specifying the OutputCache directive and their related

attributes.
Programmatic Output caching - In this method, you will use the SqlCacheDependency object

programmatically to specify the items to be cached and set their attributes.
Cache API - In this option, you will use the static methods of the Cache class such as

Insert, Remove, Add and so on to add or remove items from the ASP.NET cache, while still

using the SqlCacheDependency object to trigger the cache invalidation.

What is AJAX?
Ajax is asynchronus javascript and xml, its a way of making an asynchronus calls to the

server application.
Is AJAX a language?
ajax is a concept not a langauge.
What is the difference between syncronus and asyncronus?
syncronus, is where u make a request and wait for responce, whereas asynchronus is where u

make a request, and move ,, the response for the request may reach u later, where in u may

or may not handle the responce.
What is an Assembly?

The .NET assembly is the standard for components developed with the Microsoft.NET. Dot NET

assemblies may or may not be executable, i.e., they might exist as the executable (.exe)

file or dynamic link library (DLL) file. All the .NET assemblies contain the definition of

types, versioning information for the type, meta-data, and manifest. The designers of .NET

have worked a lot on the component (assembly) resolution.

There are two kind of assemblies in .NET; private and shared. Private assemblies are simple

and copied with each calling assemblies in the calling assemblies folder. Shared assemblies

(also called strong named assemblies) are copied to a single location (usually the Global

assembly cache). For all calling assemblies within the same application, the same copy of

the shared assembly is used from its original location. Hence, shared assemblies are not

copied in the private folders of each calling assembly. Each shared assembly has a four part

name including its face name, version, public key token and culture information. The public

key token and version information makes it almost impossible for two different assemblies

with the same name or for two similar assemblies with different version to mix with each

other.

An assembly can be a single file or it may consist of the multiple files. In case of

multi-file, there is one master module containing the manifest while other assemblies exist

as non-manifest modules. A module in .NET is a sub part of a multi-file .NET assembly.

Assembly is one of the most interesting and extremely useful areas of .NET architecture

along with reflections and attributes, but unfortunately very few people take interest in

learning such theoretical looking topics.