Page 1 of 1
PHP Replace help
Posted: Sat Dec 27, 2003 2:44 pm
by quietk

What im trying to do is replace text on a website with links and color like when you do a php code on here... here is what i have:
Code: Select all
<?
$replaceArray = Array(
// example links
"Google"=>'<a href="http://google.com" target="_blank">Google</a>',
"MSN"=>'<a href="http://msn.com" target="_blank">msn</a>',
// color coding
"<?"=>"<font color=red><?</font>",
"<?php"=>"<font color=red><?php</font>");
?>
Ok but how do i make it change that when the page loads?
thanks if you can help.
Jesse
Posted: Sat Dec 27, 2003 3:29 pm
by devork
I think you can use Output buffering ob_* functions to do this
ob_start(),ob_end_clean(),ob_flush(), ob_end_flush() , ob_end_clean().
-just control the output and where you find specified word replace them!
ob
Posted: Sat Dec 27, 2003 9:32 pm
by quietk
Ok lets say i use this:
Code: Select all
<?php
function callback($buffer) {
// replace all the apples with oranges
return (ereg_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges with bananas and some grapes.
</body>
</html>
<?php
ob_end_flush();
?>
How do i do more of them like exampe changing the work '
bananas' to '
yellow bananas' and the word '
grapes' to '
purple grapes'.
something like that how would i do that?
Posted: Mon Dec 29, 2003 5:48 am
by devork
you want to do multiple replacement?
Posted: Mon Dec 29, 2003 6:43 am
by m3mn0n
For syntax highlighting use [php_man]highlight_string[/php_man]()
For your BBcode/Smilies needs:
Code: Select all
<?php
$replaceArray = Array(
// example links
"Google"=>'<a href="http://google.com" target="_blank">Google</a>',
"MSN"=>'<a href="http://msn.com" target="_blank">msn</a>');
foreach ($replaceArray as $old => $new)
{
$thestring = str_replace($old, $new, $thestring);
}
?>
Reference material: [php_man]foreach[/php_man](), [php_man]str_replace[/php_man]()