Category Archives: Regexps

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 »

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 »