Announcement

Collapse
No announcement yet.

Changing %20 in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing %20 in PHP

    OK, So I have a nice script set up that outputs what's playing in iTunes to a templated PHP page. And just for shits, I made it so each Artist - Song Title links to allmusic.com's search page.

    Problem: AM.com doesn't use %20, they use |. for example:
    http://www.allmusic.com/cg/amg.dll?P...20Decemberists

    http://www.allmusic.com/cg/amg.dll?P...e|Decemberists

    So my question: How do i set the PHP script to output spaces as | instead of %20 ?
    NOSTALGIA IN THE WORST FASHION

    internet de la jerome

    because the internet | hazardous

  • #2
    rawurldecode()

    php.net/rawurldecode
    8:I Hate Cookies> a gota dágua foi quando falei q eu tinha 38 anos e estava apaixonado por uma garota, mas a família dela n deixava agente namorar
    8:I Hate Cookies> aí quando todo mundo me apoiou falando q o amor supera tudo, falei q a garota tinha 12 anos
    8:I Hate Cookies> aí todos mudaram repentinamente de opinião falando q eu era um pedófilo
    8:AnImoL> esses amigos falsos
    8:SCHOPE NORRIS> o amor supera tudo. da até pra esperar a puberdade
    8:I Hate Cookies> sim... fiquei desiludido schope...

    Comment


    • #3
      Well I'm mainly looking for a hunk of code that would transform the %20's to |.

      Code:
      <?php
      
      $filename = "tracks.txt";
      
      //length issues
      function truncate ($string,$maxlength) { if (strlen($string)<=$maxlength) {
                  return $string; } else { return substr($string,0,($maxlength-1))."..."; }
      }
      
      //write info
      function addHistory($track, $artist,$genre, $album){
          global $filename;
          $handle = fopen ($filename, "a+") or die ("$filename does not exist");;
          $time = mktime();
          $line= "$track\t$album\t$artist\t$genre\t$time";
          fwrite($handle, $line ."\n");
          fclose($handle);
      }
      
      //info
      function gettracks(){
          global $filename;
          $handle = @fopen ($filename, "a+") or die ("$filename does not exist or is not writeable" );;
          while (!feof($handle)) {
             $contents .= fgets($handle, 4096);
          }
          fclose($handle);
          $tracks = preg_split('/\n/', $contents);
          array_pop($tracks);
          $tracks = array_reverse($tracks);
          $i=0;
          foreach ($tracks as $song) {
              $i++;
              $song = preg_split('/\t/', $song);
              $track[title] = $song[0];
              $track[album] = $song[1];
              $track[artist] = $song[2];
              $track[genre] = $song[3];
              $track[playtime] = $song[4];
              $songs[] = $track;
              if ($i>5) break;
          }
          return $songs;
      }
      
      
      //display
      function dispTracks(){
          $tracks = gettracks();
          if ($tracks !=""){
              foreach ($tracks as $song)
                  {
                  $playdate = date("F j, Y, g:i a", $song[playtime]);
                  print "<a href=\"#\" title=\"$song[title] - $song[album] - $song[artist]\n$song[genre] - $playdate\">".truncate($song[title],30)." - ".truncate($song[artist],20)." </a><br>";
                  }
              }
          else echo "Empty Tracks.txt file";
      }
      
      //
      $add = $_GET["add"];
      $track = $_GET["t"];
      $artist = $_GET["a"];
      $genre = $_GET["g"];
      $album = $_GET["al"];
      
      
      //magic
      if ($add ==1){
          addHistory($track,$artist,$genre,$album);
      }else
          dispTracks();
      
      ?>
      NOSTALGIA IN THE WORST FASHION

      internet de la jerome

      because the internet | hazardous

      Comment


      • #4
        ah sry i didnt read nor understand properly ur question

        to replace | for spaces

        $urlcleanvar = str_replace('|',' ',$urlvar);

        to replace | for %20

        $urlcleanvar = str_replace('|','%20',$urlvar);

        to replace %20 for |

        $urlcleanvar = str_replace('%20','|',$urlvar);

        to replace space for |

        $urlcleanvar = str_replace(' ','|',$urlvar);


        Ive looked ur code and didnt find the link to allmusic.com

        there is only a print with a link to # ?
        8:I Hate Cookies> a gota dágua foi quando falei q eu tinha 38 anos e estava apaixonado por uma garota, mas a família dela n deixava agente namorar
        8:I Hate Cookies> aí quando todo mundo me apoiou falando q o amor supera tudo, falei q a garota tinha 12 anos
        8:I Hate Cookies> aí todos mudaram repentinamente de opinião falando q eu era um pedófilo
        8:AnImoL> esses amigos falsos
        8:SCHOPE NORRIS> o amor supera tudo. da até pra esperar a puberdade
        8:I Hate Cookies> sim... fiquei desiludido schope...

        Comment


        • #5
          Originally posted by schope
          ah sry i didnt read nor understand properly ur question

          to replace | for spaces

          $urlcleanvar = str_replace('|',' ',$urlvar);

          to replace | for %20

          $urlcleanvar = str_replace('|','%20',$urlvar);

          to replace %20 for |

          $urlcleanvar = str_replace('%20','|',$urlvar);

          to replace space for |

          $urlcleanvar = str_replace(' ','|',$urlvar);


          Ive looked ur code and didnt find the link to allmusic.com

          there is only a print with a link to # ?
          yes, the "#" becomes the allmusic link

          print "<a target=\"new\" href=\"http://www.allmusic.com/cg/amg.dll?P=amg&opt1=1&sql=$song[artist]\" title=\"$song[title] - $song[album] - $song[artist]\n$song[genre] - $playdate\">".truncate($song[artist],30)." - ".truncate($song[title],40)." </a><br>";

          thanks, schope
          Last edited by Jerome Scuggs; 10-11-2004, 08:20 PM.
          NOSTALGIA IN THE WORST FASHION

          internet de la jerome

          because the internet | hazardous

          Comment


          • #6
            Jacen Solo> %20

            GGGEEEEEEEEEEEEEEEEEEEEEE
            Ban Ikrit

            Comment


            • #7
              asdfghjkl, i have no clue how this works :/
              NOSTALGIA IN THE WORST FASHION

              internet de la jerome

              because the internet | hazardous

              Comment


              • #8
                man its weird to figure out what do you want to print out

                but i can help you to code that quickly if you show me the format link to open allmusic.com and the php file online, also would be cool if you could save it also on .phps format.

                try to talk with me on ss if you want some help.
                8:I Hate Cookies> a gota dágua foi quando falei q eu tinha 38 anos e estava apaixonado por uma garota, mas a família dela n deixava agente namorar
                8:I Hate Cookies> aí quando todo mundo me apoiou falando q o amor supera tudo, falei q a garota tinha 12 anos
                8:I Hate Cookies> aí todos mudaram repentinamente de opinião falando q eu era um pedófilo
                8:AnImoL> esses amigos falsos
                8:SCHOPE NORRIS> o amor supera tudo. da até pra esperar a puberdade
                8:I Hate Cookies> sim... fiquei desiludido schope...

                Comment


                • #9
                  Ever tried ereg_replace()?
                  www.php.net
                  Endless space, endless exploration.

                  Comment


                  • #10
                    Jerome is NEW!
                    <evil beavis and butthead laugh>
                    hehe hehe hehe heehehhheheheheh
                    </evil beavis and butthead laugh>
                    5: Da1andonly> !ban epinephrine
                    5: RoboHelp> Are you nuts? You can't ban a staff member!
                    5: Da1andonly> =((
                    5: Epinephrine> !ban da1andonly
                    5: RoboHelp> Staffer "da1andonly" has been banned for abuse.
                    5: Epinephrine> oh shit

                    Comment


                    • #11
                      Originally posted by Jacen Solo
                      Jacen Solo> %20

                      GGGEEEEEEEEEEEEEEEEEEEEEE
                      haha

                      Comment


                      • #12
                        Originally posted by 1ight
                        Ever tried ereg_replace()?
                        www.php.net
                        ereg_replace uses way more machine resources than str_replace, its usefull when you got a regexp to search and replace
                        8:I Hate Cookies> a gota dágua foi quando falei q eu tinha 38 anos e estava apaixonado por uma garota, mas a família dela n deixava agente namorar
                        8:I Hate Cookies> aí quando todo mundo me apoiou falando q o amor supera tudo, falei q a garota tinha 12 anos
                        8:I Hate Cookies> aí todos mudaram repentinamente de opinião falando q eu era um pedófilo
                        8:AnImoL> esses amigos falsos
                        8:SCHOPE NORRIS> o amor supera tudo. da até pra esperar a puberdade
                        8:I Hate Cookies> sim... fiquei desiludido schope...

                        Comment

                        Working...
                        X