[SOLVED] Help with Preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

[SOLVED] Help with Preg_replace

Post by hunterhp »

Hi. I've been trying to preg_replace the same string with different values each time, but preg_replace stores the first value it was given, and only the first value. When I try changing it(Trying with a loop, and tried manually too) it keeps the same value. So does str_replace.

Is there some way I can change the value of the string I replaced so that it replaces it with another value.

I'm only saying it cause I want to change {name}(from an html page, sort of like a template page) with the name of each tournament from the database.

Here's the code I'm using.

Code: Select all

$query = "SELECT * FROM tournaments ORDER BY id DESC";
              $tourneys = $DB->Query($query);
              while ($row = mysql_fetch_row($tourneys)) {


              if ($row) {

              $this->tournamentї'id'] = $rowї0];
              $this->tournamentї'name'] = $rowї1];
              $this->tournamentї'game'] = $rowї2];
              $this->tournamentї'standings'] = $rowї3];
              $this->tournamentї'winners'] = $rowї4];

         foreach ($this->tournament as $key => $value) {
         
                 $this->html = preg_replace("/{".$key."}/", $value, $this->html);

          }
          
          echo $this->html;

The html page($this->html) has the following code.

Code: Select all

<a href="tournament.php?id=&#123;id&#125;">&#123;name&#125;</a>
The script outputs the link to the last tournament on the database over and over again(6 times to be exact, the number of current tournaments in 'tournaments' table)

What should I do? how can I make preg_replace erase it's first value, and add another one the next loop.
Last edited by hunterhp on Tue Feb 01, 2005 12:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're overwriting the original text. Maybe you need to store off the result in a seperate variable?
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

I tried changing

$this->html = preg_replace();

with

$this->output = preg_replace(); (Note: I already did declared $output as a variable.)

And it resulted in the text not being replaced ({name} showed up as {name} ect.)

I don't understand. When I said $this->html had the value of <a href=blablabla> I meant it as it's getting it off another page, with file_get_contents, I don't know if that makes a difference or not.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$temp = $this->html;
while(...)
&#123;

...

foreach($this->tournament as $key => $value)
  $temp = preg_replace('#\&#123;' . $key . '\&#125;#', $value, $temp);

$this->output .= $temp;

&#125;
you don't need the if($row) bit.
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

I'm afraid it's still the same outcome :(

http://hunterhp.freeownhost.com/tournament.php

If you add a new tournament, that tournament then becomes repeated over and over, as the ORDER BY id is set on the query.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

can you post the complete code?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops.. move the $temp setting line into the while loop just before the foreach..
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

Awesome, that worked, thanks a lot feyd.
Post Reply