Page 1 of 1

How to Replace a String in an HTML ex: __foo__

Posted: Thu Jul 19, 2007 4:20 pm
by AGXGGX
There's this Script (Dolphin Smart Community Builder) which is an Online Community written in PHP. They use HTML Pages but most of the Script is pure PHP. When you need to change the layout of the Member's Profile, you need to edit the Profile.php of that Script, Also the HTML "page_7.html". But I saw something interesting on that Script and I would like to know how it's done.

When Browsing for Profiles let's say we open: http://www.domainname.com/profile.php?ID=3, the Profile.php launches the Page_7.html together with the Profile.php, but the Page_7.html is 80% written with strings like this: __thumbnail__, __pagetitle__, __sex__, __age__. Those strings are replaced by the Profile.php.

Let's say we would like to place the Members ID somewhere in the Profile View, We need to Place __IDM__.

__IDM__ calls this:

Code: Select all

$_page_cont[$_ni]['IDM']					= UserIDM();
and that calls the function UserIDM();

which is:

Code: Select all

function UserIDM()
{
        global $ID;
	
	$THEIDM = "$ID";
	
	return $THEIDM;
		}
And it returns the ID, let say #3.

That's a function I added, If you need to place a string on the HTML of Page_7.html let say" __whatever__", you just need to add this to the PHP

Code: Select all

$_page_cont[$_ni]['whatever']					= whatever();
and the function you want that to do:

Code: Select all

function whatever()
{
        $anythingHere = "";

}
and you're ready to Go, there's no need to add a str_replace etc.

But that only works if you place the:

Code: Select all

$_page_cont[$_ni]['whatever']					= whatever();
before this small string:

Code: Select all

PageCode();
How does the Script changes any __thing__ you place to a function that will return something maybe from the database without the need of making a srt_replace to __allthis__...

There's some function that does exactly this and not the str_replace that echoes a value or something?

You can go here and see the Full Profile.php:
http://svn.freedatinghost.com/doltree/f ... rev=0&sc=0

And the Page_7.html
http://svn.freedatinghost.com/doltree/f ... rev=0&sc=0

Posted: Thu Jul 19, 2007 5:44 pm
by Zoxive
Probably regex..

Code: Select all

 preg_match_all("/__((\w?)+)__/",$page,$match)

print '<pre>'; print_r ($match[1]); print '</pre>';
 

Mmmm I don't thinks is that!

Posted: Thu Jul 19, 2007 7:00 pm
by AGXGGX
The point is calling a function with: __whatever__

My HTML Code is:

<div>Hello __all__</div>

The PHP Interpretation is:

__whatever__ will be calling this function: whatever(); { $anything = "PEOPLE"; return $anything; }

and returning $anything to Replace __all__ with the Functions Result! which is PEOPLE

So __all__ is Replaced with PEOPLE in the HTML Page!

Posted: Thu Jul 19, 2007 11:54 pm
by feyd

An Example?

Posted: Fri Jul 20, 2007 12:31 pm
by AGXGGX
How to Replace __THIS__ with a mysql_query using: preg_replace_callback() + create_function() + function_exists()

let say I Have __THIS__ and I want that to be replaced with the result of this Function

Code: Select all

function this ()

{

$take = mysql_query ("SELECT 'thestring' FROM 'databasename' WHERE 'data' = '1'");

return; $take

}
It's just an example...

Posted: Fri Jul 20, 2007 5:51 pm
by feyd
I won't write the code for you. You'll have to do the primary legwork. So, what have you tried?

Ok...

Posted: Fri Jul 20, 2007 6:21 pm
by AGXGGX
I tried this ( I used another thing...)

Code: Select all

foreach ($repa[0] as $key => $value) 
	{
	    $templ = str_replace ( "__${key}__", $value, $templ );
		echo $templ;
	}


$repa[0]['title']		= title();

//
function title()
{
$reti = mysql_query("SELECT `TITLE` FROM `WEB` WHERE `THEPAGE` = '1'");
$right = mysql_fetch_assoc($reti);
return "" . $right['TITLE'];
}
//
But now an error says: Warning: Invalid argument supplied for foreach() in home... on line 14.