chriscloyd Posted May 4, 2012 Share Posted May 4, 2012 I am trying to make a presentation generator, using information from my database; my str_replace is not working please help.... here is my code <?php $Sql = "SELECT * FROM csm_players WHERE uid = '{$this->Uid}'"; $Query = mysql_query($Sql); $Style = $this->UserInfo['playerstyle']; while ($p = mysql_fetch_assoc($Query)) { $name = explode(" ",$p['name']); $PlayerName = (strpbrk($Style, '{FIRSTNAME}') ? str_replace("{FIRSTNAME}",$name[0],$Style) : ""); $PlayerName1 = (strpbrk($Style, '{LASTTNAME}') ? str_replace("{LASTTNAME}",$name[1],$PlayerName) : ""); $PlayerName2 = (strpbrk($Style, '{NICKNAME}') ? str_replace("{NICKNAME}",$p['nickname'],$PlayerName1) : ""); if ($p['cl'] == 1) { if ($this->UserInfo['clba'] == 1) { $Show .= "<div>{$this->UserInfo['clstyle']} {$PlayerName}</div>"; } else { $Show .= "<div>{$PlayerName} {$this->UserInfo['clstyle']}</div>"; } } else { $Show .= "<div>{$PlayerName}</div>"; } } //$Show .= "Error"; return $Show; this is what its returning Shelton "{NICKNAME}" {LASTNAME} [CL] Lindsey "{NICKNAME}" {LASTNAME} Eitan "{NICKNAME}" {LASTNAME} Aake "{NICKNAME}" {LASTNAME} Hunter "{NICKNAME}" {LASTNAME} if i take out the firstname search then it find the nickname and if i take out the nickname and the first it shows the last one.... what do i do? i have tried to put them in an array and do a foreach with keys and val but that doesnt work either im at a standstill. it works for my other script but not for this... Quote Link to comment https://forums.phpfreaks.com/topic/262051-str_replace-not-working/ Share on other sites More sharing options...
darkfreaks Posted May 4, 2012 Share Posted May 4, 2012 it is best if using smarty php yo use the replace function in smarty to accomplish this. Quote Link to comment https://forums.phpfreaks.com/topic/262051-str_replace-not-working/#findComment-1342914 Share on other sites More sharing options...
kicken Posted May 4, 2012 Share Posted May 4, 2012 Your final result after running all three replacements is stored in $PlayerName2 but your outputing $PlayerName, which only contains the result of the first replacement. There's no need to use three separate variables, you can just re-use the same one. You can also put your replacements in an array and do it in one call. $replacements = array( '{FIRSTNAME}' => $name[0] , '{LASTTNAME}' => $name[1], '{NICKNAME}' => $p['nickname'] ); $PlayerName = str_replace(array_keys($replacements), array_values($replacements), $Style); Quote Link to comment https://forums.phpfreaks.com/topic/262051-str_replace-not-working/#findComment-1342920 Share on other sites More sharing options...
chriscloyd Posted May 4, 2012 Author Share Posted May 4, 2012 got it to work <?php public function getPlayers() { $Sql = "SELECT * FROM csm_players WHERE uid = '{$this->Uid}'"; $Query = mysql_query($Sql); while ($p = mysql_fetch_assoc($Query)) { $Style = stripslashes($this->UserInfo['playerstyle']); $name = explode(" ",$p['name']); $array = array ( "{FIRSTNAME}" => $name[0], "{LASTNAME}" => $name[1], "{NICKNAME}" => $p['nickname'] ); foreach ($array as $key => $val) { $Style = str_replace($key,$val,$Style); } if ($p['cl'] == 1) { if ($this->UserInfo['clba'] == 1) { $Show .= "<font size=\"1\">{$this->UserInfo['clstyle']} {$Style}</font><br>"; } else { $Show .= "<font size=\"1\">{$Style} {$this->UserInfo['clstyle']}</font><br>"; } } else { $Show .= "<font size=\"1\">{$Style}</font><br>"; } } //$Show .= "Error"; return $Show; } Quote Link to comment https://forums.phpfreaks.com/topic/262051-str_replace-not-working/#findComment-1342942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.