Jump to content

str_replace not working


chriscloyd

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/262051-str_replace-not-working/
Share on other sites

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);

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;
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.