Page 1 of 1

Replacing part of a HTML file

Posted: Wed Jun 08, 2005 10:36 pm
by MathewByrne
Hi,

I'm developing a content management system at the moment and needed help with the following. I have a set of html files which have tags inside them surrounded by curly brackets {} eg:

Code: Select all

<html>
<head>
   <title>{pageTitle}</title>
</head>
...
What I want to do is load the page as a string into a variable, search through the string, replacing all instances of {.*} with a variable from an array which has the same name as keys, eg:

Code: Select all

// This variable should replace {pageTitle} in the html file
$varArray[$pageTitle] = "Title of this page.";
The end result I want is:

Code: Select all

<html>
<head>
   <title>Title of this page.</title>
</head>
...
Any help is appreciated.

Posted: Wed Jun 08, 2005 10:53 pm
by Burrito

Code: Select all

<?
$string = &quote;<title>{pageTitle}</title><body>{pageBody}</body>&quote;;
$arr = array();
$arrї'{pageTitle}'] = &quote;This Title&quote;;
$arrї'{pageBody}'] = &quote;This Body&quote;;

foreach($arr as $key => $value){
$string = str_replace($key,$value,$string);
}
echo $string;

?>

Posted: Wed Jun 08, 2005 11:00 pm
by MathewByrne
Thanks, that works well. The only other thing that I want is to find all other instances of {.*} and replace them with an empty string, so that if the array does not have {thisVariable} or whatever in it then it just removes the {} tag.

So for instance:

Code: Select all

<body>
{replaceThis}
{notInArray}
</body>
With the array:

Code: Select all

$arr[replaceThis] = "Replace This";
Would become:

Code: Select all

<body>
Replace This

</body>

Posted: Wed Jun 08, 2005 11:04 pm
by Burrito
I"m sure there's a better way than this, but I'm tired and need to pack for a trip so this'll have to do:

Code: Select all

foreach($arr as $key => $value){
  if($key == "{.*}")
     $string = str_replace($key,'',$string);
  else
     $string = str_replace($key,$value,$string);
}
edit: doh! just realized that {.*} won't be in the array...told you I was tired.

just do a str_replace on the string outside of the foreach loop and replace it with ''...that should do it.

Posted: Wed Jun 08, 2005 11:11 pm
by MathewByrne
Hmm , maybe if someone could just give the regex to replace all instances of strings between curly brackets with an empty string. :?:

Posted: Wed Jun 08, 2005 11:17 pm
by Burrito

Code: Select all

$string = preg_replace("#{.*?}#","",$string);

Posted: Wed Jun 08, 2005 11:23 pm
by MathewByrne
Thanks a heap, problem solved.

Posted: Thu Jun 09, 2005 6:27 am
by Chris Corbyn
Burrito wrote:

Code: Select all

$string = preg_replace("#{.*?}#","",$string);
{ and } are reserved characters in regex.. you should always escape them if using them literally :wink:

Posted: Thu Jun 09, 2005 6:33 am
by Syranide
d11wtq wrote:{ and } are reserved characters in regex.. you should always escape them if using them literally :wink:
just a reminder, remember as PHP needs escaping as well you need to do "\\{" etc (for safety), as otherwise PHP would interpret the text "\{" and possibly turn it into something else.