Searching within file and str_replace()ing

Hi Guys,

I am trying to work with a very simple templat ing kind of system which

1:fopen() a local .tpl file
2:str_replace <!--links1--> with some content
3:output

I can do the above without a problem, but I would like to extend the above and need your help there...becaues I think I will need some RegEx...and I suck at RegEx's.Plus I would like your input as this is something i have not done till now, but you more experenced guys might have....

1: Basically, I want to be able to specify how many records to fetch like so
<!--links1 25-->

then i can query the db accordingly to fetch 25 records....


2: I would like to have categories like so:

***show_options.tpl***
<html>
<body>
And here are some cartoons:
<!--cartoons 25-->
<br><br>
Or if  you prefer full movies:
<!--movies 15-->
</body>
</html>

so I guess I will need a regex that can check the file and see if categories exist according to the array, eg:

$categories=array("cartoons","movies","jokes","pictures","yummy","mickey","donald","batman");

if <!--cartoons 23--> is found then
$found_cats=array
(
cartoons =>"23" // the number of cartoons to query the db
)


These are just my ideas....I would appreciate any comments you have or if you have any arguements why they are not good please tell me.
eg:
Do you think I will have a problem if the .tpl files are big? (eg: >= 90kb)
Regex will be too slow...
Mag, you're too ugly... :-)
etc


Thanks,
Mag
LVL 2
mag1c1anAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Offcourse i could be wrong about this, but isn't the most important reason for using templates to separate php from html/ processing from output.

Therefor it would be inappropriate to have templates tell you processing how to act. In my opinion not templates should say the number of elements it wants to be read from database, but configuration (static or session based).

If you would like to use your templates in such a way, please use plain php then; i.e.: use <?php tpl_get('links',25); ?> instead of <!--links 25-->.

My 2cts.

-r-

0
mkortlevenCommented:
First you need to get the ids and record counts, than getting the data and at last replace and display it.
RegEx are extremely fast, if you code them correct ;-)

Getting the ids and the record counts:

$found = array();
if (preg_match_all('#<!--\s*([^>\s])\s+(\d+)\s*-->#', $fileContents, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
        $found[$match[0]] = $match[1];
    }
}

Get the data:

$data = array();
foreach ($found as $key => $count) {
    $data[$key] = /* YOUR data as HTML string for $key with $count rows */
}

Replace the "tags":

// create RegEx tags
$regExTags = array();
foreach ($found as $key => $count) {
    $regExTags[] = sprintf(
            '#<!--\s*%s\s+%d\s*-->#',
            preg_quote($key, '#'),
            $count
        );
}
$fileContents = preg_replace($regExTags, $data, $fileContents);


Is this what you mean?

Marc
0
mag1c1anAuthor Commented:
Hi Roonaan,
Thanks for replying.

I do need the template file to tell the php file what records to get etc as the end user will be makeing his own .tpl files and specifying how many records he wants returned from which category.

can you explain a bit more on how to use this:
<?php tpl_get('links',25); ?>


***

Marc,
Thanks for replying, what you wrote looks like total gibberish to me :-) but i'll take your word that RegEX's are fast if used correctly...I dont know any better.
I'll run your code get back to ya.

Cheers,
Mag
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

mag1c1anAuthor Commented:
Hi again Marc,

Its now working..... below is how I tried testing it, where am I going wrong?

*** start code ***

<?php
$fileContents=<<<blah
<html>
<body>
And here are some cartoons:
<!--cartoons 25-->
<br><br>
Or if  you prefer full movies:
<!--movies 15-->
</body>
</html>
blah;

$found = array();
if (preg_match_all('#<!--\s*([^>\s])\s+(\d+)\s*-->#', $fileContents, $matches, PREG_SET_ORDER)) {echo "in preg_match";
    foreach ($matches as $match) {
        $found[$match[0]] = $match[1];
            echo "in first foreach";
    }
}

//Get the data:

$data = array();
foreach ($found as $key => $count) {
    $data[$key] = "\$key was: $key and \$count was: $count";/* YOUR data as HTML string for $key with $count rows */
      echo "In second foreach() ".$key. " ".$count;
}

//Replace the "tags":

// create RegEx tags
$regExTags = array();
foreach ($found as $key => $count) {
    $regExTags[] = sprintf(
            '#<!--\s*%s\s+%d\s*-->#',
            preg_quote($key, '#'),
            $count
        );
}
$fileContents = preg_replace($regExTags, $data, $fileContents);
print_r($fileContents);
?>


*** End code ***

Thanks,
Mag
0
mag1c1anAuthor Commented:
Oops, I meant "not" working, it does not seem to be 'catching' any of the tags...

-Mag
0
suresh_aspCommented:
Try this,

please input database parameters
  username, password and your database

Template file -- template.tpl
--------------------------------

<html>
<body>
And here are some cartoons:
<!--cartoons 2-->
<br><br>
Or if  you prefer full movies:
<!--movies 5-->
</body>
</html>

---------------------------------
assuming database tables

create table movies( links varchar(100) );
      and insert some links like

insert into movies values("link1.html");
insert into movies values("link2.html");
insert into movies values("link3.html");

create table cartoons( links varchar(100) );
      and insert some links like

insert into cartoons values("link1.html");
insert into cartoons values("link2.html");
insert into cartoons values("link3.html");

now check it out
-------------------

<?php
$content = join("",file("template.tpl"));  // check template.tpl path

$categories=array("cartoons","movies","jokes","pictures","yummy","mickey","donald","batman");

foreach ($categories as $value){
      $pattern = "/<!--".$value." (\d+)?-->/";
      if (preg_match($pattern,$content) == 1)
            preg_match($pattern,$content,$matches[]);
}

// connect to database
$dbc = mysql_connect("localhost", "user", "password") or die("Could not connect: " . mysql_error());
mysql_select_db($database_name, $dbc);  

foreach ($matches as $key =>$value){
      preg_match("/\w+/",$value[0],$res);
      $query = "select links from ".$res[0]." limit ".$value[1]."\n";

      $result = mysql_query($query);
      $count = 0;
      while ($rows = mysql_fetch_array($result))
      {
            $links["$res[0]"][$count] = $rows['links'];  // database links accorting to category
            $count++;
      }
}

$urls = "";
$head = "";
$index = 0;

foreach ($links as $key => $cat){
      $head = "<b>".$key."</b><br>";
      foreach ($cat as $key => $value){
            $urls .= "<a href='$value'>".$value."</a><br>";
      }
      $content = str_replace($matches[$index][0],$head.$urls,$content);
      $index++;
      $urls = "";
}

echo $content;
?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mag1c1anAuthor Commented:
Hi,
Thanks for replying, your solution was not *exactly* what I was looking for but thankfully I was able to modify it to my needs, I just needed your regex's which i didnt know how to write. So since your solution helped me solve my problem and I used the code from your solution...full points to you!

Thanks,
Mag
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.