preg_match_all

Here is a sample html file contains only "p" and "div" tag:

And we want to insert content into the html, we don't want undermine the html structure, we wish to delimit it in terms of html element, not just characters. We also want to know the positions we want to insert the content. We can use PREG_OFFSET_CAPTURE:

Simple PHP performance profiling

One day you found that the page takes almost one minute to load. And it not happened before. You don't know which part of your program get a performance bottleneck. But some complex sql queries looks suspicious.

You want to know how much time elapsed for each of these queries.

Display output of PHP print_r in HTML

Both echo and print_r are widely used in day to day PHP programming, but raw print_r is very unfriendly to HTML display which is the de facto output interface in web developing environment.

Most PHPer may have tried at some moments in their career life to make their own version of a wrapper of print_r to make it HTML friendly. Here are some handy code that I used for my tasks, they are truly great life savers at some particular moments.

Most PHPer may have tried at some moments in their career life to make their own version of a wrapper of print_r to make it HTML friendly. .

How to match domain name with regular expression in PHP

There are many ways to tell whether a request is sent by bot or crawler, one of them is check the user agent string.

Normal user agent only contains information about browser version and platform information. Crawler has some special characteristics.

How to insert Adsense in the middle of content with PHP

When adding Adsense box to your website there are a lot of choices. You can put it on the banner, or the sidebar or inside the content, for the first and second its trivial to do, this post talking about the third one, inside post content, nowadays most publisher choose to put Adsense right inside the content.

How to filter out spammer website request with PHP regular expressions

Recently I got a lot spammer requests from my site, most of them are comment bot or various crawler bot. Which is cluttering my site visitor statistics panel. After some research I come up with some regular expression that can identify these requests.

Here is the function:

How to create seo friendly url for article title

Using a SEO friendly url slug for post title is common for most sites these days, most blog software like Wordpress support it. A slug url is more friendly to search engine also very convenience for your site visitors.

Nowadays, almost all search results you get from Google has such kind of url, for example

The curl error couldn't connect to host

Get this error when trying to request a web page from a PHP host using PHP curl API ... curl_exec returns FALSE curl_error is :couldn't connect to host Verbose information: * About ... out * couldn't connect to host * Closing connection #0 I try to retrieve the page in local ... . Request browser is OK. And of course the PHP web host have access to internet. So this error tells me

Functional programming in PHP and closure

One of the greatest feature of closure is treat it as an object. As known as "Closure is poor man's object".

It can make your code more elegant.

How to check IP address in an IP range with PHP

Sometimes we need to check whether an IP address located in an IP range. For example, here are two IP ranges belong to "Blue Coat" company, if it shows in Server log, means user access your website through proxy of "Blue Coat".

This post shows you how to detect IP address in IP ranges.

Previous Page 1 2 3 ... 5 Next Page