Category Archives: PHP Solutions

Regular Expression to Extract all E-mail Addresses from a File With PHP

Sometimes you need to extract some data from text files. E-mails, passwords, just some simple tags… no matter what it is, your best choice to do this is to use regular expressions. I will show you a PHP script that will extract all valid e-mails from a text file. <? $fs=fopen(“best.txt”, “r”); $f3=fopen(“clean.txt”, “a”); while(!feof($fs)) { $gan=fgets($fs); preg_match(“/[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))/”,… Read More »

PHP Function to Generate a Human Readable Word

Sometimes you need to generate some random strings, that should be readable. A good example is CAPTCHAs: they look much better if there is something like a word on them, not just some random symbols. I’ve created a function (don’t judge me for the code, I know it could be much simpler, but it was created some years… Read More »

How to Convert HTML Link Code into BBCode With PHP

Another short function appears on my blog. If you need to convert your HTML code, containing links only, into BBcode, you need this function: function make_bbcode($normal) { $samurl=str_replace(“<a href=”, “[url=”, $normal); $samurl=str_replace(“</a>”, “[/url]”, $samurl); $samurl=str_replace(“>”, “]”, $samurl); return($samurl); } That’s a very simple solution that doesn’t cover all the instances of HTML code. If you need a good… Read More »

How to Use Double Variables in PHP Scripts

I’m coming with a small piece of code that contains a sample of double variable usage. Let’s take we have a script that transmits lots of parameters, named activ1, activ2, activ3… activn. How should we work with them? A simple solution is pasted below. for ($i=0; $i<10; $i++) { $varname=”aktiv”.$i; if (isset($$varname)) { // Do anything you want… Read More »

How to Create a Simple Navigation Like Next-Previous with PHP

We often need to create something for navigation. For example, we have a gallery that consists of 400 pictures and we’d like do create 40 pages to list 10 pictures per page. In this sample we will need to create a navigation bar that will allow us to switch between pages easily. It is ok to have 40… Read More »

Regular Expression to Parse Text Between Simple Tags (XML)

It is often necessary to extract text from a variable that contains HTML or XML code. I’ve created a simple regular expression that will help you to extract all text between certain tags into an array. It is a PHP solution, though regular expression is compatible with other programming languages. preg_match_all(“/<tag>(.*?)</tag>/”, $source, $results); This construsion will create an… Read More »

Function to Extract The Value From a String With PHP

If you’re working with text information, you often need to extract some parts of text from articles, web pages and so on. You can use regular expressions for this, but if you’re not familiar with them, you’ve got to use string functions of PHP. When I didn’t know what regular expressions are, I’ve written a function that extracts… Read More »

How to Create All Possible Word Combinations from a String With PHP

Sometimes you need to change the word order in a line for some reason. I am going to show you a PHP solution that allows you to create all possible word combinations from a string. The code below should explain you all operations that are performed. <? // Factorial function fact($s){ if ($s==0) return 1; else return $fact… Read More »

Human Readable Date Format List for PHP date Function

PHP offers a large variety of arguments that can be passed to the date() function. You are able to almost everything you want having just the current time stamp. I’ve selected the most useful readable formats – I think you might need it for any purpose. I used it to randomize a site’s content to vary posting dates.… Read More »

How to Create a db4 Database from a Text File or Array

When you need to deal with big amounts of data, MySQL is not a good solution. db4 is much faster and easier – you don’t need any SQL queries to extract the necessary values and use them. The process of db4 file creation is quite easy; you need to have your PHP compiled with dba support. If you… Read More »