Jump to content

Archived

This topic is now archived and is closed to further replies.

Hooker

Preg_replace()

Recommended Posts

Im taking over development for a friends site that was all html in the past, he has about 1000 pages of html news and doesnt want to loose anything in the transfer to php, obviously i dont want to spend a few days/weeks going through each seperate html file copy and pasting bits from old news articles so i was thinking about using something along the lines of preg_replace, ive used it a couple of times for things like bb code because the only things that seem to stay constant in the files are the tags around things like "posted by" and "date" etc so i was thinking something like this: (the source would be copied and pasted into a form then this would process it)

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
$output = file("$_POST['input']");
$output = explode("|", $output);

$find = array(
"'\[b\](.*?)\[/b\]'is",
"'\[link\](.*?)\[/link\]'i",
"'\[link=(.*?)\](.*?)\[/link\]'i"
);

$replace = array(
"<strong>\\1</strong>",
"<a href=\"\\1\">\\1</a>",
"<a href=\"\\1\">\\2</a>"
);

preg_replace($find,$replace,$output);

echo $output[1];[/quote]
obviously this is just a quick example of a way to process BB code, my question is - is there anyway to save the value of the "(.*?)" 's to a csv file or a txt file so i can load it into a db onces theyre all processed?

thanks in advance.

Share this post


Link to post
Share on other sites
Yes, by using preg_match_all:

[a href=\"http://www.php.net/preg_match_all\" target=\"_blank\"]http://www.php.net/preg_match_all[/a]

Then just play with the matches array.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.