note 31945 deleted from function.preg-match by sniper
| From: | sniper@php.net | Date: | Fri, 17 Oct 2003 03:52:30 +0000 |
| Subject: | note 31945 deleted from function.preg-match by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58734@lists.php.net to get a copy of this message | ||
Note Submitter: avrosmalenNOFUCKING@SPAMhotmail.com
----
A comprehensive guide on (Perl) regular expressions:
[http://www.wdvl.com/Authoring/Languages/Perl/
PerlfortheWeb/pattern_matching.html]
I inserted an example below for a search & change (this time not a search & delete as shown
lots of time in the other comments).
If you want to do a search & change you can use the following example:
Say I want to search a html file and change all the direct links to point to a template file with an
added variable. The variable would be the original name of the html file (the (\w+) block). It would
look like this:
$file = "some_file.html";
$search_string = "/(<a href=")(\w+)(\.html)/i";
$replace_string = "\${1}template_file.php?pagename=\$2";
$contents = file($file);
foreach ($contents as $line_num => $line) {
if (preg_match('/(<a href=")(\w+)(\.html)/i', $line)) {
echo ($line_num. " before: ". $line. "<br>");
$line = preg_replace($search_string, $replace_string, $line);
echo ($line_num. " after: ". $line. "<br>");
}
}
Make sure you use double quotes in the $replace_string or the blocks $1 and $2 will not be replaced
with the original contents.
Add a piece of code that 'walks' all the html files in a directory and you could use it to
move entire (static) websites into a database in just one go.
Later,
Tone