So i’ve been working on my website in which i’d like to add a random quote each time the page is loaded from bash.org, i Kinda got it working but i was thinking about using “preg_replace” instead of “array_splice” to get rid of the header and footer of the page. With the code im currently using it get rid of just the header and i cant figure out how to get rid of the footer. Please Help!
Code i currently have
<html>
<head>
<title>Chris's Website</title>
<link href="lesson_3_css.css" type"text/css" rel="stylesheet"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css"> body, a, a:hover {cursor: url(http://q3ait.org/~bogauschc/WorkInProgress/test/proxy/cur116.cur), progress; scrollbar-3dlight-color:#FFFFFF; scrollbar-arrow-color:#FFFFFF; scrollbar-base-color:#FF9900; scrollbar-darkshadow-color:#333333; scrollbar-face-color:#FF9900; scrollbar-highlight-color:#FFFFFF; scrollbar-shadow-color:#000000;}
body {
background-image: url(Speaker%20Fire.png);
}
.title div {
color: #F00;
}
</style>
<script language="JavaScript">
function clock(){
var time = new Date()
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var ampm = " PM "
if (hour < 12){
ampm = " AM "
}
if (hour > 12){
hour -= 12
}
if (hour < 10){
hour = " " + hour
}
if (minute < 10){
min = "0" + minute
}
if (second < 10){
second = "0" + second
}
document.clockForm.clockBtn.value = hour + ":" + minute + ":" + second + ampm
setTimeout("clock()", 1000)
}
window.onload=clock;
</script>
</head>
<body>
<div class="title">
<div align="center">
<p> </p>
<td align="left"><form name="clockForm">
<input type="button" name="clockBtn" value=" " />
</form>
<a><img src="http://www.reliablecounter.com/count.php?page=bogauschc.no-ip.org&digit=style/plain/4/&reloads=0" border="2"></a>
</br>
</div>
</div>
<div class="nav">
<p><a href="http://q3ait.org/~bogauschc/html">HTML files</a></p>
<p><a href="http://turntable.fm/mayor_bees_room">My Turntable</a></p>
<p><a href="http://q3ait.org">Q3AIT</a></p>
<p><a href="http://www.chathamcentralschools.com/hs/">Chatham High</a></p>
<p><a href="http://q3ait.org/~bogauschc/WorkInProgress">Work In Progress</a></p>
<p><a href="games.html">Games</a></p>
<p><a href="webcams.html">Webcams</a></p>
</div>
<div class="main" align="center">
</br>
<p>Im Chris and I Can Do Things!</p>
<p> </p>
<?php
if(!$_GET['n']) {
$num = rand(1,400);
$open = fopen("http://bash.org/?".$num, "r");
$line = file("http://bash.org/?".$num);
array_splice($line, 0, 83);
array_splice($line, 89, 200);
foreach ($line as $line_num => $line) {
echo $line;
}
}
?>
<p> </p>
<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fpages%2FChriss-Website%2F218161641601802&width=292&height=62&colorscheme=light&show_faces=false&border_color&stream=false&header=true" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:292px; height:62px;allowTransparency="true"></iframe>
<p> </p>
<script language="JavaScript">
VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";
</script>
<script language="JavaScript" src="http://www.hashemian.com/js/visitorIP.js.php"></script>
<p><a href="IndexOf.html">Index Of Files For This Page</a></p>
</div>
</body>
</html>
here is an example of a bash quote http://bash.org/?47
My website is http://q3ait.org/~bogauschc/
Thats the entire index.php, if you can help get rid of the header and footer it would be much appreciated!
—
Chris
First things first – this is a bad way to do this. On the fly is exceptionally bad as ceejay pointed out in his comment. However –
That should give you just what’s in the qt class, which appears to be what you want.
The RIGHT way to do this – besides asking them for permission to use their content since they don’t appear to offer an API or rss feed – would be to run this on the back end and store every unique quote you get into your own database and serve from that on page load instead of remote URL. You could do this on a cron that runs at a reasonable interval or just write a script and run it yourself.
Once again – bad idea since you don’t own the content. That’s essentially theft.
** this is also considering you said you are ok with changing your code if they change their dom. parser would be better **
preg_match_all() http://php.net/manual/en/function.preg-match-all.php
args: pattern, subject, matches
pattern is the regex you intend to use to cross reference the next arg:
subject is the contents returned from file_get_contents in this case. It can also just be any string.
matches is the variable you assign the results to. It can be whatever you want, so
should result in an array like this:
since the numbers 2 and 1 in the string are the only thing that matched the \d+ (digits only) regex pattern I provided.
Regex can appear horrifying but don’t get discouraged. Basic google searches usually help you find the pattern you need. Or you can ask here.
By the way, you can also just use preg_match() since you are looking for only one result. I just didn’t know if they came on a page with many results so I went with preg_match_all().
to avoid empty results, you can just throw a conditional in there:
Please note this example above is for preg_match. Preg_match_all will return a multidimensional array, I believe. user basic debugging tools like print_r to widdle this down to what you need.
Also, I am using this example under the assumption that you are running this on a back end script not initiated by the user. If you want an on the fly option, you should look into ajaxing this request, then making a recursive function that would re-call the bash site if there were no results.