Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.

GUIDE:: Output Replacing

Output Replacing

I got a good lesson in Output Replacing from PHP-Fusion Lead Developer Starefossen a few months back and now it's time for me to share this knowledge.

For those of you not familiar with Output Replacing I would like to share with you what I have learned and I will break down everything Hans (Starefossen) has taught me in my lesson.

No More Editing Core Files
With Output Replacing this means you do not have to edit any core files you only need to tell the code where to output and voilà your mod is running without modifying a single file.

Some of this information is from [url]http://code.starefossen.com/infusions/fusion_functions/replace_in_output
[/url]

Quote


replace_in_output - Replace any string in the output

Quote

Description

CodeDownload  
replace_in_output ( string $target, string $replace, string $modifiers )



With this function, you can replace any string in the output using a regular expression replacement. The function will construct a preg_replace() function call from the parameters like this: $output = preg_replace("^$target^$modifiers", $replace, $output);

Quote


Parameters

target

Quote

What you want to replace


replace

Quote

This is what it will be replaced with


modifiers

Quote

The pattern to search for as a string.


Quote

Return Values

This function will not return anything!


Quote

Example#1
Basic replace_in_output() usage

 $target = "<!--news_prepost_2-->";
$replace = "<!--news_prepost_2-->\n<br />Hello world!";
replace_in_output($target, $replace);



This will insert "Hello world!" right before the second news item if this code is added to a panel and displayed at the news.php page. Below is how the source code will look like when using the default PHP-Fusion theme - Gillette.

Output to Example#1

CodeDownload  
</tr>
</table>
<!--news_prepost_2-->
<br />Hello World!
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>




--------------------------------------------------------------------------------------

Quote

Starefossen made this example for us to use.
This example panel will display at the bottom of each Article.

When this panel is enabled it will only be visible where you have asked it to be placed.

Now we know what page we want this panel we need to show the panel where exactly to display on the page.

To do that we do the following...

In this example we use it on articles.php

Quote

First we check if the current page is articles.php

CodeDownload  

if (FUSION_SELF == "articles.php") {



Quote

To be able to modify the output by using replace_in_output() everything needs to be stored in a variable - there for we have to start output buffering and catch all code in a variable.

CodeDownload  
ob_start();



Quote

Then we do this...

We include the file, all included files will be stored and not sent as output so you can include or echo as much text you want here

CodeDownload  
 openside($locale['ads003']);
    echo "<div align='center'>";
    echo "Panel Code Here";
    echo "</div>";
    closeside();




Quote

After that we store the output in a variable so we can insert it to the replace_in_output() function.

CodeDownload  
$ob_get_contents = ob_get_contents();



Quote

Then we end output buffering so things can work normal from here.
CodeDownload  

ob_end_clean();




Quote

To prevent replace_in_output() to returning error messages we have to replace all ' with \'

CodeDownload  
$ob_get_contents = str_replace("'", "\'", $ob_get_contents);




Quote

Finally we replace.
The Panel will output after each article.

CodeDownload  

   replace_in_output("<!--sub_article-->", "<!--sub_article-->\n".$ob_get_contents);



Quote

Dont forget the closing } at the bottom...

CodeDownload  
}



Quote

When this panel is complete it should look like this...
[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]
if (FUSION_SELF == "articles.php") {
ob_start();
openside($locale['ads003']);
echo "<div align='center'>";
echo "Panel Code Here";
echo "</div>";
closeside();
$ob_get_contents = ob_get_contents();
ob_end_clean();
$ob_get_contents = str_replace("'", "\'", $ob_get_contents);
replace_in_output("<!--sub_article-->", "<!--sub_article-->\n".$ob_get_contents);
}[/syntaxhighlighter]


Quote

Test the above code by pasting it in a centre panel and enabling it.
Then take a look at one of your Articles at the bottom you will see our example output.

Play around with it and see what ideas you can come up with. It's a fun function to use.

I have already used it to make a Ad's Panel which is available to download from the ADDONS DB.
Our Ad's on the bottom of news and articles are placed there using the Output Replacing function. All done without having to edit one core file. :)

Check Articles, News or Custom Pages over at Fangree Productions to see the Ads in action.
very useful guiding

thanks
hehehe... This is really very Good and Nice thing man... :D

Yeah ! First time, I was confused very hard :@ while using your ADS Panel :P that How did you made changes in the viewthread.php without making any changes ? :P LOL !

Anyways, really Good function... Thanks to you and Hans for sharing it... ;)
This is just an example on how to do it externally.

[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]<?php
function gamer_info($tag)
{
$tag = str_replace(" ", "%20", $tag);
$url = 'http://gamercard.xbox.com/en-US/'.$tag.'.card';
$a = file_get_contents($url);
preg_match("/\<div class\=\"Stat\"\>([0-9]+)<\/div>.*/is", $a, $matches);
$gamerscore = @$matches[1];
if ( $gamerscore == "--" || !$gamerscore )
{
die("Invalid Gamertag"wink;
}

unset($matches);

preg_match("/\<span class\=\"Gold\"\>([A-Za-z0-9 ]+)\<\/span\>/is", $a, $matches);
$gamertag = @$matches[1];
unset($matches);

preg_match("/\<img class\=\"GamerPic\" width\=\"([0-9]+)\" height\=\"([0-9]+)\" src\=\"(.*?)\" (.*?) \/\>/is", $a, $matches);

$gamerpic = @$matches[3];

preg_match_all("/\<img class\=\"Game\"(.*?) title\=\"(.*?)\" \/\>/is", $a, $matches);

$games = Array();
$i = 0;

foreach ( $matches[2] as $game)
{
$games[$i]['Title'] = $game;
$games[$i]['Image'] = $matches[0][$i];
$i++;
}

$info = Array(
"Gamerscore" => $gamerscore,
"Gamertag" => $gamertag,
"Gamerpic" => $gamerpic,
"Games" => $games
);

return $info;
}
$info = gamer_info("oOGLITCHKINGOo"wink;
$games = unserialize($row['user_games']);
then
foreach ( $games as $game )
{
echo $game['Image'];
}
echo $game_str;
?>[/syntaxhighlighter]

Copyright by ME. grin
Very usefull.

just one question. Where does the:
CodeDownload  
$target = "<!--news_prepost_2-->";
$replace = "<!--news_prepost_2-->\n<br />Hello world!";
replace_in_output($target, $replace);


- belong in the above example from Fangree Craig ? I don't see they are used..
its very powerful. I use it all the time. with the properly coded theme you can insert panels on the fly. I've done it for myself as well as a few others.

@craig - FYI: If you use ob_get_clean() rather than ob_get_contents() then you don't need to call ob_end_flush()

or you can make more calls - which is exactly what you don't want to do. exactly why its best to limit the replacement calls. anyway, good post, people should find it useful if they actually read...
Thanks Craig, it's very useful guiding. I studying on it. I hope that I understand and I get on fine.

Merged on Mar 31 2012 at 04:34:30:
If I want to use link with picture in there, How do I have to use?
I tried many examples like both of them on below but did not :S
[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]if (FUSION_SELF == "news.php") {
ob_start();
echo "<a href='news.php?readmore=".$data['news_id']."' title='".$data['news_subject']."'><img src=".IMAGES_N_T.$data['news_image_t2']." height='80px' width='100px' alt='".$data['news_subject']."' /></a>";
$ob_get_contents = ob_get_contents();
ob_end_clean();
$ob_get_contents = str_replace("'", "\'", $ob_get_contents);
replace_in_output("<!--news_prepost_2-->", "<!--news_prepost_2-->\n".$ob_get_contents);
}[/syntaxhighlighter]

and

[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]$target = "<!--news_prepost_2-->";
$replace = "<!--news_prepost_2-->\n<br /><a href='news.php?readmore=".$data['news_id']."' title='".$data['news_subject']."'><img src=".IMAGES_N_T.$data['news_image_t2']." height='80px' width='100px' alt='".$data['news_subject']."' /></a>";
replace_in_output($target, $replace);[/syntaxhighlighter]

Merged on Mar 31 2012 at 04:46:02:
If I use link and image, how do I have to use?
Did you resolve it mehmet?

Quote

Ankur wrote:

hehehe... This is really very Good and Nice thing man... :D

Yeah ! First time, I was confused very hard :@ while using your ADS Panel :P that How did you made changes in the viewthread.php without making any changes ? :P LOL !

Anyways, really Good function... Thanks to you and Hans for sharing it... ;)


Is Forum_bot_ads panel based on this aswell?
Yes it was one of the first infusions to use output replacement, as was the Forum Extension Panel. Both Wooya and Max were ahead of time. hehe

Quote

Craig wrote:

Yes it was one of the first infusions to use output replacement, as was the Forum Extension Panel. Both Wooya and Max were ahead of time. hehe


I have no knowledghe of PHP but now I know what output replacing means :)
Thread Information
Author
Replies
11 posts
Views
5352 times
Last Post
Last updated on 8 years ago
You can view all discussion threads in this forum.
You cannot set up a bounty in this discussion thread.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
You cannot up or down-vote on the post in this discussion thread.
Users who participated in discussion: BombJe, Craig, smokeman, NetriX, Mehmet, PHPar, Ankur, Tyler