PHP Replace help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
quietk
Forum Newbie
Posts: 7
Joined: Sun Nov 16, 2003 10:33 pm
Location: California

PHP Replace help

Post 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
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post 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!
quietk
Forum Newbie
Posts: 7
Joined: Sun Nov 16, 2003 10:33 pm
Location: California

ob

Post 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?
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

you want to do multiple replacement?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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]()
Post Reply