Skip to content
Advertisement

How to split text string in PHP (Song – Artist)

I have this bit of PHP

 <?php
//get artist/title info
$artist = $_GET['artist'];
$title = $_GET['title'];

//create a temp file to store values for AJAX script
$r = fopen("temp_title.txt", "w");
fwrite($r, $artist." - ".$title);
fclose($r);

?>

I want it to show on my page as:

Song
Artist

How would I make the song appear in bold and what would I put in place of the ” – ” to make a line break instead?

Advertisement

Answer

Assuming you want to generate some HTML that can be picked up by a AJAX call, the simple way to do what what you want is

   fwrite($r, "<b>$artist</b><br>$title");

However, this doesn’t fall strictly within web semantics. What you probably should use is

   fwrite($r, "<h1>$artist</h1><br>$title");

(Or some variation of h1 to h6)

For more control over the actual appearance, use a class and some CSS

 fwrite($r, "<h1 class='artistName'>$artist</h1><br>$title");

You’ll need to add a little CSS to your page to get the artist name in bold:

<style>
   h1.artistName {
        font-weight:bold;
        font-size:150%;
}
</style>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement