Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
MathewByrne
Forum Commoner
Posts: 38 Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia
Post
by MathewByrne » Wed Jun 08, 2005 10:36 pm
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.
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Wed Jun 08, 2005 10:53 pm
Code: Select all
<?
$string = "e;<title>{pageTitle}</title><body>{pageBody}</body>"e;;
$arr = array();
$arrї'{pageTitle}'] = "e;This Title"e;;
$arrї'{pageBody}'] = "e;This Body"e;;
foreach($arr as $key => $value){
$string = str_replace($key,$value,$string);
}
echo $string;
?>
MathewByrne
Forum Commoner
Posts: 38 Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia
Post
by MathewByrne » Wed Jun 08, 2005 11:00 pm
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>
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Wed Jun 08, 2005 11:04 pm
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.
MathewByrne
Forum Commoner
Posts: 38 Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia
Post
by MathewByrne » Wed Jun 08, 2005 11:11 pm
Hmm , maybe if someone could just give the regex to replace all instances of strings between curly brackets with an empty string.
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Wed Jun 08, 2005 11:17 pm
Code: Select all
$string = preg_replace("#{.*?}#","",$string);
MathewByrne
Forum Commoner
Posts: 38 Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia
Post
by MathewByrne » Wed Jun 08, 2005 11:23 pm
Thanks a heap, problem solved.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Jun 09, 2005 6:27 am
Burrito wrote: Code: Select all
$string = preg_replace("#{.*?}#","",$string);
{ and } are reserved characters in regex.. you should always escape them if using them literally
Syranide
Forum Contributor
Posts: 281 Joined: Fri May 20, 2005 3:16 pm
Location: Sweden
Post
by Syranide » Thu Jun 09, 2005 6:33 am
d11wtq wrote: { and } are reserved characters in regex.. you should always escape them if using them literally
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.