I have already told about string handling. And in string handling I contented myself with exact entry of substring to whole string. I think, it`s time to extend our abilities. I was studing regular expressions last several day and now I am ready to tell you something about it. Using this splendid thing you can set up any pattern you want to search for in the string. We will be using only one php function to deal with regular expressions:
preg_match(”pattern”, “subject”, “matches array”);
How does regular expressions mechanism work? Very simple. preg_match() function passes pattern and subject to search mechanism and fills mathes array with results.
Popularity: 43%
I’ll continue discussing an exceptions handling theme which was begun in the earlier article: PHP Exceptions. Part 1. At the end of those post I wrote that I didn’t show you some features. Let’s consider them now. Note that all presented exceptions features are right only for php5.
User-defined exceptions
We know that there is common Exception class that covers absolutely all types of exceptions. Until now, many of you didn’t know that you can define your own exception types. This mechanism founded at classes inheritance. I hope you have already learnt about inheritance in one of my previous posts of my blog.
So, let’s try to create our own exception type. Let’s call it MyException:
Popularity: 39%
I already wrote about an xml parsing, but today I found out, that there is a much more pleasant way to pull out data from XML. This way is based on use of SimpleXML extension which is accessible in PHP since 5-th version.
In general, you probably have noticed, that I concern only 5-th version of PHP in the last articles, not considering older versions. I think we shouldn’t discuss an old things
Now, practically all hostings support PHP5.
So, let’s consider the SimpleXML extension. It appears that pulling out data from XML as easy as shelling pears now. For example, we have a file with the following structure describing the book:
Popularity: 37%
I have written a lot in this blog already. But I didn`t touch php security subject yet. But it is, in my opinion, on of the most important subject from each web-devepoler. Script has to not just work. But also be on other side forever, not on deliberate criminal. Naturally it can be discussed for a long time, and this post can`t cover whole this problem in details. But I`ll give you a start.
There are many hacker attacks exist. And there many security techniques exist also. I have decided to settle on one which is simple enough. Filter inputs method.
Popularity: 46%
Why do programmers need it? Explaining… Whatever one may do, but basis of any site is content. In most cases it is text (mb. pictures also). User coming to site takes account of find information he is interested in. And founded it he bacames happy, bacause he needn`t to search anymore. But as the practice shows, in most cases, he does it vaintly. There some problems he can face with. For example, crooked presentation of information.
Lets imagine, we looking for answer for any scientific question (palms propagation). Finding some site with not corresponding title, but its snippet contains ‘palm’ word. Following it and seeing 150kb text, which we will scroll only for 2 minutes at least and starting think smth. like: “oh, fuck”, “so many letters”, “maybe I should looking for just ‘palm’ word” etc. After such long text visitor can become irritable and leave the site. It is evident that this fact decreases site popularity.
There two solutions at least.
First: Site owner orders content-manager to split whole text into chapters and paragraphs, and create separate page for each of it using CMS.
Second: Split text into pages automatically, using script. As it done in wordpress - there are additional navigation in the bottom of each page with pages numbers.
Popularity: 38%
This post is about polymorphism at last. We don’t know this term but we’ll try to state it ourself using our experience. Firstly, note that polymorphism is a main part of the object-oriented programming. The programming cannot be object-oriented if it have not polymorphism technics and possibilities.
So, what is it?
Generally speaking, when I thought about this term I found that polymorphism is some object’s ability to change its form or to use many different forms. Something like that
I’m discussing in terms of my own C# programming experience.
Popularity: 35%
In some forums, guestbooks or other similar scripts, where users can post their messages, usual input field is not enough. People have got into the habit of different get-ups like underline and bolded fonts. As it made in editors like MS Words and others.
It not only makes message more attractive, but also adds readability and usability to it (for example, if it is necessary to add link of any site, but not only its address).
Features like these are used everywhere now. But how can we implement it? Exactly it I`ll tell you today.
First that comes to mind is - allow users to input html-code in input fields. In other words, if user wrote something like:
“Hi to all! I didn`t see so loathsome site for a long time. Bye.)” befor, then now he can write smth. like:
“Hi to all! I didn`t see so <b>loathsome</b> site for a long time. Bye.)”.
After this message will be outputted in other users browsers, it will be like:
“Hi to all! I didn`t see so loathsome site for a long time. Bye.)”
All anything, but this way has at least 2 disadvantages.
Popularity: 86%
Cookie mechanism is necessary to store some data on client`s side. For example, when browser offers you to save your login / password for any site, it stores them in cookies. It is reasonable that cookies for different sites store in different files. For example, IE browser stores all cookies in text files in Cookies folder. Cookie file name depends on your system account name and domain you have visited.
By the way, I have made experiment. I have visited site which containes poll and have voted there. After this cookie file was created. My second attempt to vote the poll was refused. I became angry and deleted cookie files. After this site didn`t recognize that I have voted already. But it is not always truth. Some systems use more advanced security like ip-based antiflood.
Let`s have some practice now.
To create cookie, you have to use setcookie(’name’, ‘value’, time_to_live). I have made a little exmaple that demonstrates cookie mechanism.
Popularity: 43%
PHP5 classes can be inherited, i.e. inherited class can acquire parent’s properties and methods. What are we need it for?
Let’s consider an inheritance of people, for example. I’m human and I inherit some properties of the people class. For example, there are a talking capability, intelligence, water and food requirement. These properties are not unique for any individual human but they are inherent for all people. Note that «Human» class inherits a water, food and air requirement from the «Mammal» class. In turn, this class inherits these properties from «Animals» class.
Thanks to the inheritance, we can save much time describing a real object. For example, ask me: what is a duck? I’ll answer: it is a bird that quack. I said a few words but these words provided insight into this bird. I made you understand that a duck inherents all properties of any bird and in addition «quack» property, when I said «it is a bird that quack»
Popularity: 66%
Today we’ll discuss PHP5 exceptions mechanism. What is an exception? All first-time programmers are faced with it some day.
An exception is some problem accured during a script works. Simplify it is just a program error. Why was it called «exception»? What’s the difference between a simple error and an exception?
Usage
Let’s see the next peace of code:
Popularity: 50%