Forum Moderators: coopster

Message Too Old, No Replies

str_replace not working

         

electricocean

12:22 am on Aug 19, 2005 (gmt 0)

10+ Year Member



Hi,

I made script that gets values from a db and outputs them with a pipe character ( ¦ ) in between the values like so:

$query = "SELECT DISTINCT genre FROM jokes";//and the rest you know

echo "<p align=\"center\">";
while ($row = mysql_fetch_array($result)) {
$genre_pipe = "<a href=\"?pageid=2\">". ucfirst($row['genre']) ."</a>";
$genre_pipe .= " ¦ ";
$genres = str_replace("¦ ¦", $replace, $genre_pipe);
/*test if it works:

if($genres === true){
echo $genres;
}
else{
echo "bad one";
}

*/
echo $genres
}
echo "</p>";



it outputs "Funny ¦ Scary ¦ Yo Mama ¦ ¦" 2 extra ¦ ¦ at the and i i want to chop um off!

When I say echo $genre it prints out everyhting like it would be w/o str_replace().

And when I tried the if statement to see if it worked it always echoed "the bad one" over and over.

before i did the str_replace() i did substr($genre_pipe, 0, -1) which did not work for some reason

Thanks,
electricocean

bibby

3:38 am on Aug 19, 2005 (gmt 0)

10+ Year Member



I don't see $replace mentioned before ti is called.

Here's an alternate without str_replace.
///

$query = "SELECT DISTINCT genre FROM jokes";
echo "<p align=\"center\">";

$list='';$first=0;

while ($row = mysql_fetch_array($result)) {
if ($first==0){$pipe='';}else{$pipe=' ¦ ';}
$list.="$pipe"."<a href=\"?pageid=2\">". ucfirst($row['genre']) ."</a>";
$first++;

/*test if it works:
if($genres === true){echo $genres;}
else{echo "bad one";}
*/
echo $list}echo "</p>";
------------------------
This way you build a string rather than editing one. There is no pipe before the 1st item ,or after the last, since the pipe is placed before the link, but skips the first one. Pipes a have a once space pad on each side and are not included in the link.
Hope with works better.