Page 1 of 1

Replacing words in different files

Posted: Tue Oct 11, 2011 4:37 pm
by EcazS
Basically I want a back-end file that searches for the string "{SEARCH_STRING}" and then replace it with code.

So,
[syntax=php]
check_file_for_given_string({SEARCH_STRING});

function replace($string) {
$strings = array("{SEARCH_STRING}", "{TEMPLATE}", "{BLABLA}");
if(in_array($string, $strings)) {
if($string == "{SEARCH_STRING}") {
replace_the_found_string_with(<?php include("core/func/widget/search_results.php"); ?>);
}
}
}
[/syntax]

If only there were a 'replace_the_found_string_with' function and a 'check_file_for_given_string' function, a man can dream... Thoughts on how this could be done, in an efficient way?

Obviously I code code myself a function that searches through a file or something like that, for the first function that is.

Also, did the highlighter break or something?

Re: Replacing words in different files

Posted: Tue Oct 11, 2011 6:11 pm
by jacek
Do you want to replace the string with the code directly, or with the result of the code ?

Also, the syntax highlighting is fine, it just bugs out a little if the code you use has <?php or ?> in it.

Re: Replacing words in different files

Posted: Tue Oct 11, 2011 6:29 pm
by EcazS
The results of the code, I think at least.

Say the string was {TEMPLATE}, I would want the page to display information from a database but the code to fetch the info from the database would be in a different file... If you understand. So basically all that would exist in the index file would be the basic XHTML template and inside the body there would be {TEMPLATE} but when you view the page it would have content from the DB.

Re: Replacing words in different files

Posted: Tue Oct 11, 2011 7:15 pm
by jacek
Well the way that is generally done is to get the content into a string and then use a simple str_replace

[syntax=php]echo str_replace('{MAIN}', $content, file_get_contents('template_file.html'));[/syntax]
for example.

Re: Replacing words in different files

Posted: Tue Oct 11, 2011 7:26 pm
by EcazS
Alright, thanks. I'll give that a go.

I'm thinking of using this for the CMS I'm doing. That way it will be easier for "outsiders" to make their own designs and such. It shouldn't affect the speed so much right? It's a relatively small "step" to process, I hope at least :lol:.

Re: Replacing words in different files

Posted: Tue Oct 11, 2011 8:25 pm
by jacek
I'm not really sure, it adds to the number of file operations which is one of the slower things.

I think you should be fine, a lot of the big CMS / forums use that method.

Re: Replacing words in different files

Posted: Sat Dec 03, 2011 12:17 pm
by EcazS
I just now had time to experiment a little bit with this but then I realized it's gonna be pretty hard doing it a "neat" way.
Right now I have it set up like this,
[syntax=php]
$path = file_get_contents("template/xhtml.temp");
$loads = array(
'title',
'content'
);
foreach($loads as $load) {
$template = str_replace('{' .strtoupper($load). '}', "Hello", $path);
}
echo $template;
[/syntax]
And this is the XHTML file,
[syntax=xhtml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>{TITLE}</title>
</head>
<body>
{CONTENT}
</body>
</html>
[/syntax]

Now, obviously if I echo the template variable inside the loop it's going to output the template twice which is...just wrong. If I have it outside the loop it only replaces the last value in the array, {CONTENT}.

My question is, how would I replace all the {THINGS} without outputting the template more than once?

Re: Replacing words in different files

Posted: Sat Dec 03, 2011 7:05 pm
by jacek
You can use the array options that str_replace has

[syntax=php]echo str_replace(array('1', '2'), array('3', '4'), $something);[/syntax]
will replace all 1 with 3 and 2 with 4.

Re: Replacing words in different files

Posted: Sat Dec 03, 2011 7:54 pm
by EcazS
Ooh, I didn't know I could use arrays with str_replace. Learn something new everyday...
Thanks a lot! Now it's working perfectly :D