Replacing part of a HTML file

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Replacing part of a HTML file

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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;

?>
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post 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>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Hmm , maybe if someone could just give the regex to replace all instances of strings between curly brackets with an empty string. :?:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$string = preg_replace("#{.*?}#","",$string);
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post by MathewByrne »

Thanks a heap, problem solved.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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.
Post Reply