Jump to content

[SOLVED] str_replace not working properly??


rule69

Recommended Posts

Can some1 tell me what im doing wrong here....

i dunno why it doesnt wana work properly :/... ???

it should print this:

 

artist: artist1
track: title1
cat: genre1

artist: artist2
track: title2
cat: genre2

artist: artist3
track: title3
cat: genre3

 

but instead its doing it own thing by printing this out:

 

artist: a
track: t
cat: g

artist: r
track: i
cat: e

artist: t
track: t
cat: n

 

<?php	 $url = '
http://somethng.com/dir/artist1_-_title1_-_genre1_.mp3\n
http://somethng.com/dir/artist1_-_title2_-_genre2_.mp3\n
http://somethng.com/dir/artist1_-_title3_-_genre3_.mp3\n
'; 
  $urly = explode("\n", $url);
   
   
    $forms = 3;
for($i=1;$i<=$forms;$i++){
$link = $urly[$i];
$lins = explode("\n",$link);
list($artist, $track, $cat) = explode('-', strrchr($link, '/')); 


  echo "<br />artist: ".unchar($artist[$i])."<br />
  track: ".unchar($track[$i])."<br />
  cat: ".unchar($cat[$i])."<br />"; 
  	  } 


?>

 

thanks in advance

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

<?php
function unchar($text)
{
//Unwanted chars
$char = array("%26", "%28", "%2C", "%27", "%5B", "%5D", "%20", "/", "_");
//Wanted Chars
$repl = array("&", ")", ",", "(", "[", "]", " ", "", " ");
//Replace Unwanted with Wanted
$text = str_replace($char, $repl, $text);

return $text;
}
?>

a function i made for cleaning encoded chars....

These lines are the culprit:

<?php
  echo "<br />artist: ".unchar($artist[$i])."<br />
  track: ".unchar($track[$i])."<br />
  cat: ".unchar($cat[$i])."<br />"; 
?>

 

You are telling PHP to just print one character from each string. Remove the "[$i]"

<?php
  echo "<br />artist: ".unchar($artist)."<br />
  track: ".unchar($track)."<br />
  cat: ".unchar($cat)."<br />"; 
?>

 

Ken

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.