0

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>&nbsp;</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>&nbsp;</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>&nbsp;</p>
<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fpages%2FChriss-Website%2F218161641601802&amp;width=292&amp;height=62&amp;colorscheme=light&amp;show_faces=false&amp;border_color&amp;stream=false&amp;header=true" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:292px; height:62px;allowTransparency="true"></iframe>
        <p>&nbsp;</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

6
  • 1
    Doing this is going to make your page run really slow, especially if Bash.org is down.
    – ceejayoz
    Jan 17, 2012 at 21:00
  • 2
    I would recommend load the QotD via ajax AFTER the page has loaded. Jan 17, 2012 at 21:01
  • @ceejayoz i havent noticed that it is running slowly with the current setup but do you have any knowledge into using preg_replace? Jan 17, 2012 at 21:05
  • 2
    You should use a html parser instead of preg_replace. It will be easier to write and maintain.
    – greg0ire
    Jan 17, 2012 at 21:06
  • 1
    Using preg_replace is a bad idea. If they change their HTML format it's going to spooge all over your page.
    – ceejayoz
    Jan 17, 2012 at 21:06

2 Answers 2

2

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 -

$data = file_get_contents('http://whatever-bash-url-is.php');
preg_match_all ("/<p class=\"qt\">([^`]*?)<\/p>/", $data, $matches);
print_r($matches);

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

preg_match_all("\d+", 'long string containing 2 expected digits to match this 1 pattern', $results);

should result in an array like this:

$results = [
    [0] => 2,
    [1] => 1
];

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:

foreach($matches AS $m)
{
    if($m)
    {
        //process only if there is something here
    }
}

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.

3
  • Thanks for the help.. I'll see how this goes. Jan 17, 2012 at 21:20
  • alright so i got it on but it says "Array ( [0] => Array ( ) [1] => Array ( ) )" on every quote.. which it shouldnt. and how would i go about doing it if the quote didnt exist? not all of the numbers it picks actually has a quote. Thanks for the Help! Jan 17, 2012 at 21:40
  • Try just using preg_match() then. I tried it on my server and the results are as expected.
    – Kai Qing
    Jan 17, 2012 at 21:51
2

@ceejayoz is right, it will make your page run slower to scrape bash.org on the fly like that. You're better off scraping their page separately and caching a bunch of results if you really want the quotes.

For parsing the page, rather than relying on offsets and line numbers, you might be better off using a tool like PHP Simple HTML DOM Parser to pull the appropriate contents from the file. It will make it a lot easier to handle pulling data from the pages, and will also make it a lot easier to update your script if they ever change their page layout.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.