Today is Sunday
September 5, 2010

August 3, 2010

Display Random YouTube Videos on your site or blog with PHP

by Chris — Categories: Coding, PHP — Tags: , , 4 Comments

Well here is another random function from my little repository.. A little while back one of my clients presented me with the idea that they wanted to display a set of videos from there YouTube gallery at random on there home page and various other sections of there site. But only specific videos did they want. So from that I created a little function. That would do just that. I will say though this function is a bit toned down from what I actually gave them as they wanted a small control panel to go with it ran off a mysql database so they could add and remove videos from from randomly chosen videos they provided from there YouTube gallery they also wanted to provide a brief description on there page for each one as well amongst a couple other things with it. Which I may re-tailor that version of the script eventually and release it to the public as I think its a nice idea. Could make for a good 404 error page or like with them a good way to keep the content on there main page changing but also display some of there portfolio of videos at the same time.

Anyway back to the meat of this article I doubt you want to read more than you have to and lets face it, if you’ve seen my other articles here you would know writing isn’t really my forte. That said heres the code and a brief explanation and as always a demo of it working.

function randomTube(){
    $RVideoArray = array('q8SWMAQYQf0', 'q8SWMAQYQf0', 'q8SWMAQYQf0', 'hpxxvOU3vbI');
    $RVideoCount = count($RVideoArray);
    $RVideoSelect = rand(0, $RVideoCount - 1);
    $RVideoNum = trim($RVideoSelect);
    $RVideo = $RVideoArray[$RVideoNum];
    $theTube = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$RVideo.'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$RVideo.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
    return $theTube;
}

In this version of the script, I have set up an array that you can add/remove you’re choice of YouTube videos from. All you need to do is obtain the YouTube short code for the video or what ever it is they call it specificly, I call it short code cause to me it resembles a familiar look/feel to that of a short URL through something like bit.ly. But thats off topic. With almost any youtube video there is always a URL construct similar to www.youtube.com/watch?v= what follows the v= and in some cases comes before the next & is the actual bit of the URL you are looking for to use with the array $RVideoArray in the function above.

Example of usage for the Random YouTube video function is.

$myRandomVideo = randomTube(); echo $myRandomVideo;

June 27, 2010

Javascript vs PHP

by Chris — Categories: Coding, Javascript, PHP — Tags: , , , 7 Comments

This is a question I often see and get. “Which one should I use, JavaScript or php?” Well in the end it all boils down to what your doing and how your are expecting it to handle. The biggest key differences between JavaScript and PHP is one runs or renders rather while the page is loading. While the other can do stuff to a page while its loading or after it has loaded.

Both languages are very similar in construct or at least they are in my opinion, and you write code almost the same way. Obviously they have different built in functions, and names for the functions, and they differ here and there. Generally you can almost do anything with JavaScript that you can with PHP and vice versa.

Now comes again the question “Which should I use?”. Well I personally like PHP for a wide array of things and will generally use it over JavaScript when possible however with todays technology always trying to push the bar up and tempting to break the barriers of just how far you can push someones browsers abilities I do favor AJAX which is the use of PHP and JavaScript together. I say “when possible” because there are times where it definitely is not wise to use AJAX. But thats only due to security concerns. But worry not, most applications you will build with AJAX if you use it wisely and properly you wont have much to worry about. Which one day I am sure I will write an article or many about that.

If you want your elements to be flashy and less bandwidth consuming I would suggest using JavaScript where and when possible to handle your pages content and how its loaded, then handled there after. If bandwidth isn’t much of an issue for you which with most hosting providers it isn’t (unless your pushing several thousands of gigs of there bandwidth a month, then you should check the fine print in there contracts). Then handle everything via PHP its a bit more static, pages have to load but if used properly its as secure as it gets, and can run pretty fast as well.

If your interested in going the AJAX route which I highly recommend I would suggest using jQuery. As its the most stable and mature of frameworks out there and has a much larger community of developers to help get answers from if you ever get stuck.

Are you recently starting out with php, javascript, ajax, mysql, or really any web related scripting language? If you are might I suggest to you a site that helped even me learn the basics years upon years ago. http://www.tizag.com they don’t cover everything, but they cover more than just the simple basics and can give you a good footing on any given scripting language.

AJAX ready frameworks that you can choose from in no particular order (obtained from Wikipedia):

jQuery
jQuery UI (used with jQuery)
Prototype
Script.aculo.us (used with prototype)
Ext (now known as Sencha)
MooTools
Yahoo! UI Library
Dojo Toolkit

Quick Question I see with these framworks mostly due to people searching for ready made scripts is can you use more than one framework. Well ultimately it depends which you use. I know jQuery (even combined with UI) you can set it up to allow other frameworks as mentioned on the jQuery site in there article/post about the subject Using jQuery with Other Libraries however outside of that I cant really vouch for the other libraries and there support on the subject as I prefer jQuery and use that over the rest when ever possible (unless I am working on a clients site and they have something different from jQuery)

June 23, 2010

PHP create a random string of Alpha/Numeric Characters

by Chris — Categories: Coding, PHP — Tags: , , No Comments

Have a need for a quick random string of any given length but want to limit it to a-z 0-9. Then this would be ideal for you, I really can’t think of any real world use for this. Well no thats wrong, you can use it as a random password generator. Or something to the effect of that.

<?php
function genRandomString($howbig) {
$length = $howbig;
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';

for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}

return $string;
}
echo "<strong>5 char string:</strong> ". genRandomString('5') ."<br />";
echo "<strong>10 char string:</strong> ".genRandomString('10')."<br />";
echo "<strong>15 char string:</strong> ".genRandomString('15')."<br />";
echo "<strong>20 char string:</strong> ".genRandomString('20')."<br />";
echo "<strong>50 char string:</strong> ".genRandomString('50')."<br />";
?>

Example of use:
5 char string: z3gn2
10 char string: ygpxxqozvf
15 char string: ywi81tiihyvbf2
20 char string: nfuo8ksxfrbexb1sv74n
50 char string: csh0d3a2k0vygp86oe3f0ryaa452rai5yxczlyoiwujxt3sm6

June 22, 2010

PHP truncate/shorten string, variable, text

by Chris — Categories: Coding, PHP — Tags: , , No Comments

Every now and again on my projects I come into a spot where I have limited room to display any given amount of text that is generated and updated dynamically. Example I have a length of text thats 400 some odd characters long. I only have room for 120 characters. “So what do I do?” If your asking yourself that question then I have an answer for you, keep in mind not THE answer as there are better solutions out there that will trim truncate any given string down to the number you want but leaving full words in tact at the end rather then cut them off mid word. This function I will show you here that I made for myself a while back is expandable to do such things however I haven’t ran into the need or desire to expand it as such myself as it works for what I use it for without problem.

<?php function ShortenText($text, $chars) {
// Change to the number of characters you want to display
$chars = $chars;
$text = $text."";
$countchars = strlen($text);

if($countchars > $chars) {
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
}

return $text;
}
$myText = "Welcome to Monkey Tooth Productions.";
echo ShortenText($myText, 20);
?>

Translates to:
Welcome to Monkey…

It automatically appends the “…” to the string so try to remember that if you need 20 max you should set it to 17 however you can change that or remove it even if you like.

Well as with any code I throw up here on the site, if you expand this and improve it let me know. I’d be happy to see what you guys do with it.

PHP get IP Address

by Chris — Categories: Coding, PHP — Tags: , , , , No Comments

I have seen this done a hundred and one ways (figuratively speaking of course). However each way I have seen it there was always the occasion something would come up short, and the IP variable would come up undefined or empty. So it eventually lead me to having to tailor my own solution to obtain either my IP or a users IP depending on what my cause is.

Example: Your IP is: 38.107.191.106

How was this obtained? Through a rather simple process of elimination. Since there are multiple ways to get an IP address, yet not a single one of them works every time dependent on the user side of things. So what I have done is made a little function to try diffrent methods and then return the result to me. See the code below. It’s that simple. Although I am sure some of you are likely able to expand on this in a multitude of ways i’d be happy to see what come up with.

<?php
function getRealIpAddr(){

if(!empty($_SERVER['HTTP_CLIENT_IP'])){$ip=$_SERVER['HTTP_CLIENT_IP'];}

elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];}

else{$ip=$_SERVER['REMOTE_ADDR'];}

return $ip;
}
echo "Example: Your IP is: ".getRealIpAddr();
?>

PHP my domain name

by Chris — Categories: Coding, PHP — Tags: , No Comments

Need to find your domain name for a script? Maybe you want to ensure something thats going to run is being run on your site and your site alone. In concept for example trying to protect your script from XSS attacks, say in a case where your using AJAX to POST/GET JSON requests (but this is only a small example of why you might want to implement something like this).

<?php
$mydomain = $_SERVER['HTTP_HOST'];
$mydomain = str_replace('www.''', $mydomain);

if($mydomain == "your-domain-name.com"){
/*your domain found, script to run*/
}else{
/*your domain NOT found, script to run*/
}
?>

Now some people will most likely argue that you should use

$_SERVER['SERVER_NAME']

instead of

$_SERVER['HTTP_HOST']

And this is plausible as they do pretty much the same exact thing. However the key difference is how one reads off the server versus the other, I would go in to the differences but Chris Shiflett puts it together in a nice summary in his article SERVER_NAME Versus HTTP_HOST and backs it up with various tests and other articles he has found on the subject.

June 19, 2010

Home Movie Gift

by Chris — Categories: Live SitesNo Comments

Giving the perfect gift of Fun LOVING Memories.

Take the time to save your cherished memories on DVD. Forgotten old films and home movies that you haven’t seen for years, can now be watched and enjoyed to a whole new generation. Grand Dad at Christmas or mom holding the new baby all brought back to relive once again. Old videotapes or movies films are not a problem to transfer at Steve Sattler Films.” (more…)

Steve Sattler Films

by Chris — Categories: Live SitesNo Comments

“Today there is a collision between old and new media. Steve Sattler Films offers an array of Media Content Solutions.

Video is replacing signage, training, manuals, and marketing. We have the means and the expert skills to produce quality and affordable video and audio solutions for your business.

Steve Sattler is an Emmy Award winning Director and Filmmaker. He has 25 years experience in Broadcast TV, Film, Documentary and Commercial Production and is an experienced cameraman, editor and producer. Steve Sattler Films is a full service film and video production facility, located in Connecticut about 90 minutes from north east of New York City or South West of Boston.” (more…)

New York Bar and Wine Show

by Chris — Categories: Live SitesNo Comments

THE BAR & WINE SHOW is the Premier Trade Event on the East Coast…Bar None!!
The Industry Marketplace for new & classic products & services.
The Bar & Wine Show is located in the heart of New York City. Where there are more establishments that serve and sell spirits beer and wine, within 2 hours of the Jacob Javits Convention Center, than any other place on earth! New York City also has the heaviest concentration of bars, clubs, and restaurants in the world. Which gives you a formidable combination for success! The Bar & Wine Show means Business and is the only Trade Show specifically for Professionals representing the Bar, Nightclub, Restaurant, and Liquor Store Industry.” (more…)

Original GLBT Expo

by Chris — Categories: Live SitesNo Comments

“For the past 17 years, the EXPO has helped to present the finest products & services available to the GLBT consumer. Starting in 1993, the EXPO has generated over $80,000,000 of dollars spent within the Greater Tri-State area. This includes the GLBT friendly companies from every industry, both large and small, Fortune 1000 and Gay-owned companies. They have all discovered the GLBT Community is affluent, brand-loyal, well educated and business minded. It is the truest of definition of “THE PERFECT NICHE MARKET”.” (more…)

© 2010 Monkey Tooth Productions All rights reserved - Wallow theme by TwoBeers Crew - Powered by WordPress - Have fun!