?
Solved

trying to replace pattern in varible

Posted on 2005-05-05
7
Medium Priority
?
343 Views
Last Modified: 2008-03-10
OK it is creating files via the tpl file that the script is pulling in and naming them correctly but it is not replacing the pattern,
example:
$template = file_get_contents("template.tpl");
switch($template)    // <----- Is this the wrong way to do this?
  {
     case "[TITLE]":
       $template[$key] =  $original_kw;
         break;
    case "[KEYWORD]":
      $template[$key] =  $original_kw;;
         break;
  }
 

<?
$p_id = "ubtq50g";
 $servername = "localhost";
$db_name = "v6sites";
$connection = @mysql_connect("localhost", "", "")
        or die("Couldn't connect.");
@mysql_select_db($db_name, $connection)
        or die("Couldn't select database.");
?>
<?
// Locate all keywords for project spin
$sql2 = "SELECT * FROM `keyprase` WHERE `p_id` = '$p_id' ORDER BY 'd_name'";
     
         
$result2 = @mysql_query($sql2,$connection) or die("Couldn't execute domain Query<BR> $sql2.");
while ($row = mysql_fetch_array($result2)) {
        $r_keyprase = $row['keyprase'];
        $r_d_id = $row['d_id'];
               
        $keyword .= "
$r_keyprase
";
// Load the template into memory
$template = file_get_contents("template.tpl");
//$template = "";
$keyword  = trim($keyword);
$original_kw = $keyword;
$keyword = str_replace(" ","-",$keyword);
$filename = ("ads/".$keyword.".php");

//now we would replace patter with the keyword

  switch($template)
  {
     case "[TITLE]":
       $template[$key] =  $original_kw;
         break;
    case "[KEYWORD]":
      $template[$key] =  $original_kw;;
         break;
  }
 

//Open new file and writing template data to file
$myfile = fopen($filename, "w+");
fwrite($myfile, $template);
fclose($myfile);

$keyword = "";
}

?>
0
Comment
Question by:jbrashear72
7 Comments
 
LVL 9

Expert Comment

by:gruntar
ID: 13939633
>>$template = file_get_contents("template.tpl");
>>switch($template)    // <----- Is this the wrong way to do this?

This is OK but $template must return ONLY "[TITLE]" or "[KEYWORD]" otherwise nothing will happen!!

since you're using  file_get_contents("template.tpl"); i presume you have more data than just "title" or "keyword".
You have to do something to retrieve only needed data form .tpl

hope that helps
0
 
LVL 3

Author Comment

by:jbrashear72
ID: 13939717
Ok So I tried it another way:
// Load the template into memory
$template = file_get_contents("template.tpl");

$keyword  = trim($keyword);
$original_kw = $keyword;
$keyword = str_replace(" ","-",$keyword);
$filename = ("ads/".$keyword.".php");

// Replacements for template
$patterns[0] = '/[TITLE]/';
$patterns[1] = '[KEYWORD]/';

$replacements[0] = 'Looking for';
$replacements[1] = 'Keyword';

//now we would replace patterns in the loaded template file.
preg_replace($patterns, $replacements, $template);

This gives me no results.
[TITLE] and [KEYWORD] stay the same.
Is this the right track?
0
 
LVL 20

Expert Comment

by:virmaior
ID: 13940123
it could be...
but if you're doing a simple replace of [TITLE] and [KEYWORD]
then you can just str_replace with array parameters instead of using preg_replace

e.g.
$seek = array();
$replace = array();
$seek[1] = '[TITLE]'; $replace[1] = 'Looking For';
$seek[2] = '[KEYWORD]'; $replace[2] = 'Keyword';

echo str_replace($seek,$replace,$template);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:punkstar
ID: 13940156
...my script :-P

To search for the words using my technique, you need to split the whole template up into different words seperated by spaces.  This can be done by using the explode() function.  You must make sure that the tempalte is clean.  Its best not to use any indenting in the tempalte.

--how to explode
<?php
$template = file_get_contents("template.tpl");
$template = explode(" ",$template);
//now the variable $template has been turned into an array with the different lines as different elements.
for($i = 0;$i <= count($template);$i++)
{
switch($template[$i])
  {
     case "[TITLE]":
       $template[$i] =  $your_title_variable;
         break;
    case "[KEYWORD]":
      $template[$i] = $you_keyword_variable;
         break;
  }
}

//bring the template back together
$template = implode(" ",$template);
//$template now holds the page content
?>

I think that should work.  It will go through the words and replace the certain words with the variables.
0
 
LVL 9

Accepted Solution

by:
gruntar earned 1500 total points
ID: 13942772
Hi,you have easyer option. here is how you can do it

// read template into memory
$template = file_get_contents("template.tpl");

// simple replace for all fields you need
$out_string = str_replace ('[TITLE]', $replace_title, $template);
$out_string = str_replace ('[KEYWORD]', $replace_keyword, $out_string);

// new version that can be echoed or whatewer you need
echo $out_string;


hope that helps
0
 
LVL 6

Expert Comment

by:German_Rumm
ID: 13944307
Hi jbrashear72,

your replacements don't work, because square brackets "[]" are special reg-exp characters.

In order for your code to work you should define patterns like this:
    $patterns[0] = '/'.preg_quote('[TITLE]', '/').'/';
    $patterns[1] = '/'.preg_quote('[KEYWORD]', '/').'/';
    // http://php.net/preg_quote

or escape these special symbols by hand:
    $patterns[0] = '/\[TITLE\]/';
    $patterns[1] = '/\[KEYWORD\[/';

Another thind, preg_replace RETURNS modified string, not just modifies it in place, so you should've used
    $template = preg_replace($patterns, $replacements, $template);

---
German Rumm.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 14005027
When replacing using preg_replace you could also you the /e modifier:

$template_data['title'] = 'My title';
$template_data['keyword'] = 'My keyword';

$template = '<title>[title]</title><keyword>[keyword]</keyword><title>[TiTlE]</title><new>[unknownkey]</new>';

echo htmlspecialchars(
  preg_replace('#\[(\w+)\]#ie',
               'isset($template_data[strtolower("\\1")]) ? $template_data[strtolower("\\1")] : "[\\1]";',
               $template));

However, one should always remember that the most efficient, and most powerfull template engine available would actually be php itself.

Why wouldn't one just use below coding for template?
  <title><?=$title;?></title><keyword><?=$keyword;?></keyword>

-r-
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question