Regular expression used to validate URLs
08 Oct
Posted by: admin in: PHP Solutions, Regexps
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))/”, $gan, $matches);
fwrite($f3, trim($matches[0]).”\r\n”);
}
fclose($f3);
fclose($fs);
?>
best.txt is [...]
06 Sep
Posted by: admin in: Apache Performance, PHP Solutions, Regexps
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 [...]
There is a common practice to use outgoing links from your site to track visitor activity. For example, your site is www.site.com, and external links look like www.site.com/out.php?url=http://anothersite.com . It is OK for counters and traffic tracking, but may be used for an uncommon way. It this sample you may replace http://anothersite.com with any other [...]