Help with PHP - preg_replace()

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
Hi,

I'm trying to get my script to work. What I'm doing at the moment is I have a file (header.html):

HTML:
<html>
<body>
<title>%BOARD_NAME% &bull; %PAGE_TITLE%</title>
</head>
<body>
<table border="1px" width="100%">
<tr>
<td>
<h1>%BOARD_NAME%</h1>
<p>%BOARD_DESC%</p>
<p><a href="index.php">Index</a><br /><a href="viewf.php">View Forum</a><br /><a href="viewt.php">View Topic</a></p>
</td>
</tr>
<tr>
<td>
<br />

and style.php:

PHP:
<?php

include("./config.php");

function start_page($title)
{
	global $config, $func;
	$header = $func->get_include_contents("./styles/" . $config['style'] . "/header.html");
	$patterns = array('/%BOARD_NAME%/', '/%BOARD_DESC%/', '/%PAGE_TITLE%/');
	$replace = array($config['board_name'], $config['board_desc'], $title);
	echo preg_replace($patterns, $replace, $header);
}
?>

Then when start_page() it called from another page it echos header.html, but with certain information changed.

Currently I am having to type everything out manually - is there any way to get it to automatically do the arrays from what I've already got?

en.php

PHP:
<?php

$lang = array(

// Common language

		'LOGIN'    => "Log In",
		'REGISTER' => "Register",
		'WELCOME_MESSAGE' => "Welcome to the boards, we hope you enjoy as much you do",
		'COPYRIGHT' => "Copyright &copy; BLAH 2012",
		'FORUMS' => "Forums",
		'TOPICS' => "Topics"
);
?>

(these are not the full scripts, just enough to tell you what they do.)

Is there anyway way to change $lang to two arrays? Eg:

$lang = "LOGIN", "REGISTER" etc
$lang2 = "Log in", "Register" etc

I'm thinking a combination of print_f and preg_replace might do that.

If you can help then thanks :D

~Callum
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Array_keys and Array_values o exactly what you ask. How about something like this:
PHP:
<?php 
$header = $func->get_include_contents("./styles/" . $config['style'] . "/header.html"); 
$lang = array( 
        'LOGIN'    => "Log In", 
        'REGISTER' => "Register", 
        'WELCOME_MESSAGE' => "Welcome to the boards, we hope you enjoy as much you do", 
        'COPYRIGHT' => "Copyright &copy; BLAH 2012", 
        'FORUMS' => "Forums", 
        'TOPICS' => "Topics" 
); 

$keys = array_keys($lang);
$values = array_values ($lang);
echo preg_replace($keys, $values, $header);
?>
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Those three lines could actually be truncated down to one:
PHP:
echo preg_replace(array_keys($lang), array_values($lang), $header);
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
I think it still thinks I don't exist, try clicking my username and you'll see :)
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
"You must spread some Reputation around before giving it to lemon-tree again."

:p

~Callum
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
I think I repped you aaages ago in Free Hosting, cos you were being helpful :)

~Callum
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
Hmmm it doesn't work.

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /home/alexmac/public_html/style.php on line 33

http://callum.x10hosting.com/main.php echos both arrays using print_r at the top.

The source code:

PHP:
function start_page($title)
{
	global $config, $func, $lang;
	$header = $func->get_include_contents("./styles/" . STYLE . "/header.html");
	$patterns = array_keys($lang);
	$replace = array_values($lang);
	print_r($patterns);
	echo "<br />";
	print_r($replace);
	echo preg_replace($patterns, $replace, $header);
}

~Callum
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
I've worked out the problem. The first array has to be \%EXAMPLE_TEXT%\, instead of EXAMPLE_TEXT :(

~Callum
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Yeah, thought you'd already noticed that, preg needs a regex.
 
Top