str_replace giving me problems

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
Little Spy
Forum Commoner
Posts: 31
Joined: Thu Oct 10, 2002 8:18 pm
Contact:

str_replace giving me problems

Post by Little Spy »

This is a bit from a script that would take certain values in a news template. And replace them with the actual vars from the mysql db. $page_content just outputs it to the site template system think of it the same as echo

An example $newsstyle might have something like this in it

$newsstyle = "<DISPLAY: Subject><br><DISPLAY: Poster>";

however I cant get str_replace to replace the vars it just comes back with a 0. If anyone has any suggestions or knows the problem please reply. Thnx!

Code: Select all

$newsstyle = $news_settings&#1111;'newsstyle'];
   while ($data = mysql_fetch_array($query)) &#123;
      // Convert From Unix Time
      date($datestyle,$data&#1111;'date']);
      $newsstyle = str_replace($newsstlye, "<DISPLAY: Subject>", $data&#1111;'subject']);
      $newsstyle = str_replace($newsstyle, "<DISPLAY: Date>", $data&#1111;'date']);
      $newsstyle = str_replace($newsstyle, "<DISPLAY: Poster>", $data&#1111;'poster']);
      $newsstyle = str_replace($newsstyle, "<DISPLAY: Email>", $data&#1111;'email']);
      $newsstyle = str_replace($newsstyle, "<DISPLAY: ComNum>", $data&#1111;'comnum']);
      $page_content .= $newsstyle;
   &#125;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

wrong order of parameters for mixed str_replace ( mixed search, mixed replace, mixed subject)
try

Code: Select all

$newsstyle = $news_settings['newsstyle'];
while ($data = mysql_fetch_array($query)) {
	// Convert From Unix Time
	date($datestyle,$data['date']);
	$search = array('<DISPLAY: Subject>', '<DISPLAY: Date>', '<DISPLAY: Poster>', '<DISPLAY: Email>', '<DISPLAY: ComNum>');
	$replace = array($data['subject'], $data['date'], $data['poster'], $data['email'], $data['comnum']);
	$page_content .= str_replace($search, $replace, $newsstyle);
}
instead
Post Reply