I was looking for lyrics websites and i came over this one and much easy to get data from it
http://www.metrolyrics.com
http://www.metrolyrics.com
If you visit this website and search for artist and song title you will notice url will change to
http://www.metrolyrics.com/one-love-lyrics-bob-marley.html
and if you right click and get the source just scroll to where all the lyrics and its inside a div container called lyrics-body-text.
Now Logic is that get url get div and echo it out.
This project is requires the following
1. web server
2. php installed
3. notepad/or your favorite editor
In your web root folder create a folder "getlyrics" inside this folder create a empty php file name it "index.php"
right click and open it with your favorite editor and pop this code
right click and open it with your favorite editor and pop this code
<h3>T.K Lyrics</h3>
<form action="" method="POST" enctype="multipart/form-data">
Song :
<input type="text" placeholder="Enter Song Title here" name="song" /><br />
Artist :
<input type="text" placeholder="Enter Artist here" name="artis" /><br />
<input type="submit" name="submit" value="search" /><br />
</form>
As you can see this is just a simple form with no html open and closing tag but if you can want in the future when you become a master web developer you can externalize your php file and html for example.
index.html
<html>
<head>
<title>GetLyrics</title>
<style>
</stle>
</head>
<body>
<h3>T.K Lyrics</h3>
<form action="process.php" method="POST" enctype="multipart/form-data">
Song :
<input type="text" placeholder="Enter Song Title here" name="song" /><br />
Artist :
<input type="text" placeholder="Enter Artist here" name="artis" /><br />
<input type="submit" name="submit" value="search" /><br />
</form>
</body>
</html>
Here is the process.php save both in one folder
<?php
if(isset($_POST['submit'])){
$song = $_POST['song'];
$newsong = preg_replace('! !', '-', $song);
$artis = $_POST['artis'];
$newart = preg_replace('! !', '-', $artis);
$url = "http://www.metrolyrics.com/$newsong-lyrics-$newart.html";
$content = file_get_contents($url);
$getdiv = explode( '<div id="lyrics-body-text">' , $content );
$enddiv = explode("</div>" , $getdiv[1] );
echo "$song - $artis";
echo $enddiv[0];
}else{
$second_step[0] ='';
echo "----------------------------------------------";
}
?>
But for now, use this one.
After your form, pop in this php code.
More about lines:
1. <form action="" method="POST" enctype="multipart/form-data">
Form action = "" meaning to load only this page method="POST" we are posting/request to get data and for this we need enctype.
2. <input type="text" placeholder="Enter Song Title here" name="song" /><br />
Inputs type is text and come with the id/name to get data to php to process the request
3. <input type="submit" name="submit" value="search" /><br />
Button id/name to submit to php
4. if(isset($_POST['submit'])){
if user click submit button go to next code
5. $song = $_POST['song'];
$newsong = preg_replace('! !', '-', $song);
We set our variable $_POST['song']; is the data coming from the form and will store in $song. Now we need to get artis and song and make like http://www.metrolyrics.com/one-love-lyrics-bob-marley.html to do this we need to use preg_replace. Get $song and replace any space '! !' with '-' data from user will become new data with - place in space and will store in $newsong
6. $url = "http://www.metrolyrics.com/$newsong-lyrics-$newart.html";
Define our URL as you can see newsong with - in place - lyrics-newart.html.
7. $content = file_get_contents($url);
To read string from a file in php we used file_get_content for this we need to store this strings in a variable we stored them in $content and getting this string from $url.
8. $getdiv = explode( '<div id="lyrics-body-text">' , $content );
We get data from user, and pass it php, php preg replace and store and get the content so far this php getting all the content from this website all we want is to get only a data from a div with an ID to do this we need to split the string we used explode to only div id and this div coming from $content we again stored this to a vairble call $getdiv
9. $enddiv = explode("</div>" , $getdiv[1] );
We spited target div and now we need to tell php where to end the split and we place this div in a array we with one
10.
echo "$song - $artis";
echo $enddiv[0];
After data passing through preg replace, get content, get div splitting and ending split and now get this little, little, little monkeys and display them. Here echo $enddiv[0] string here now will be come array of 0 but if now data dont display any thing a-da-wais-nakami-roiu- :)
Hope yufala i lanem wan samthing but if not let me come over and put all this codes in your brain OKeeeeey?
Hope yufala i lanem wan samthing but if not let me come over and put all this codes in your brain OKeeeeey?
Next project!, as i was typing writing a-long, i came up with this..... olcem wanem sipos mi apply this getlyrics thing disply lyrics lo local video play blo mi sipos user click lo wan video, code will run and disply the lyrics nanananananan! sex on the beach!!!! laalalalal ok iam out...............................................................
No comments:
Post a Comment