make code shorter

Posted on 2011-09-07
Last Modified: 2012-05-12
hello there,
I have a little script on my website so when ppl submit new wallpapers it would scan the link to see if its online or not..
I am trying to make this code shorter and more professional, when you run the script it works fine but it needs a bit work..
what would you do?


function curl($linkA, $postfeilds = '') {
	global $pagel;

	$ch = curl_init($linkA);
	curl_setopt($ch, CURLOPT_URL, $linkA);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)");
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
	curl_setopt($ch, CURLOPT_TIMEOUT, 20);
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	if($postfeilds) {
  	curl_setopt($ch, CURLOPT_POST, 1);
  	curl_setopt($ch, CURLOPT_POSTFIELDS, $postfeilds);


function check($linkB, $regex, $pattern='', $replace='') {
    if(!empty($pattern)) {
        $linkB = preg_replace($pattern, $replace, $linkB);
		$pagek = curl($linkB);
    if(eregi($regex , $pagek)) {
        echo 'alive: '.$linkB."<br>\n"; flush();
    elseif(!eregi($regex , $pagek)) {
         echo 'down: '.$linkB."<br>\n"; flush();

$sites = array(
			array("megaupload\.com\/?", "(Filename:|File description:)","MU")

$alllinks = '

$alllinks = explode(" ", $alllinks);
$alllinks = implode("\n", $alllinks);
$alllinks = explode("\n", $alllinks);

foreach($alllinks as $linkC) {

  if(eregi("(http|www)" , $linkC)) {
		foreach($sites as $site) {
			if(eregi($site[0], $linkC)) {
			check(trim($linkC), $site[1]);
			$titleCode[] = $site[2];
		if(eregi('hotfile\.com\/dl\/', $linkC)) {
	    $hfFile[] = $linkC;
	    $titleCode[] = "HF";

if($hfFile[0]) {
	$hflinks = array_chunk($hfFile, 500);
	foreach($hflinks as $hflink) {
		$hflink = implode("\n" , $hflink);
		$postfeilds = 'files='.$hflink;
		$pagel = curl('', $postfeilds);
		preg_match_all('/Existent/' , $pagel , $match);

		$hflink = str_replace("\n", '<br>', $hflink);
		if (count($match[0]) > 0) {
			echo 'alive: ';
			echo 'down: ';
		echo $hflink.'<br>'; flush();


Open in new window

Question by:XK8ER

Expert Comment

ID: 36500106
Other than separating the functions out to a functions.php type file, I can't think of a better way to do it.  Sometimes you just require a lot of fields and a lot of text, I wouldn't get to out of shape over it being long.  

I comment the heck out of all of my code, so it's always long anyhow.
LVL 35

Expert Comment

by:Terry Woods
ID: 36500871
Note that the eregi function is deprecated. You may like to proactively convert it to use preg_match unless you're planning on not upgrading PHP at all, ever. If you'd like help converting them, just ask.
LVL 110

Accepted Solution

Ray Paseur earned 500 total points
ID: 36501992
From a computer science standpoint, making code shorter is never a reasonable objective.  Making it run faster, adding comments, upgrading to modern techniques (refactoring) are the kinds of things professionals might do.

On line 1 we have this: <? and that makes the code dependent on the "short tags" environmental variable.  Environmental dependencies are unavoidable, but are to be avoided anyway.  I would change that to <?php.  If you don't know why, try intermixing XML and PHP short tags to see the outcome.

On line 2 we have nothing, and I would add this: error_reporting(E_ALL);  It is more than a little helpful to see all the Notice and Deprecated messages.  For example, it can keep you from accidentally relying on an undefined variable.

On line 5 we have global $pagel;.  If you need a global value, it is almost always a better practice to put it into the $GLOBALS array.  Here is why that is a good idea.  Let's say there are two programmers working on the same software (and if the software is worth anything to the world there will be at least two programmers working on it!) and they must work in different offices.  The last thing you need is a variable name "collision" that causes the work of one to overwrite the variables of another.  If you inject a variable into the main scope of the script, you increase this risk.  If you use $GLOBALS instead, you create the ability to use a code scanner to look for instances of "$GLOBALS" (case-sensitive) and thereby avoid variable collisions.  The best-practices solution is probably to refactor the code into object-oriented programming, but simply getting rid of global declarations is a good start.

On line 12 we have curl_setopt($ch, CURLOPT_TIMEOUT, 20);.  Is 20 seconds realistic?  Most HTTP request complete in subsecond times.  I am very lazy and impatient.  I think I would not want to wait 20 seconds for an HTTP request to complete.

On line 24 we have return($pagel);.  See line 5.  This kind of programming evinces misunderstanding and confusion about the nature of variable scope, and that is a way of ensuring that catastrophe is not left to chance.

On line 16-19 we have a conditional code block that is misaligned.  A good programming practice is to indent conditional code blocks, lining up the curly braces so the reader can immediately see what the heck is going on.  Personally, I like to use a modified Zend standard for indenting conditional code.  I place curly braces on their own line and indent the code inside 4 spaces.  Some text editors will automatically substitute a tab for 4 spaces.  Conditional misalignment is in evidence throughout the script.

On line 21 we have this: $pagel=curl_exec($ch);.  There is no error checking or visualization.  CURL is not a black box.  It can and will fail for reasons outside of your control.  So a reasonable expectation would be that a programmer would provide some kind of error handler.  You might want to learn about curl_errno() and curl_error().

Next, let's consider this construct:
$alllinks = '

$alllinks = explode(" ", $alllinks);
$alllinks = implode("\n", $alllinks);
$alllinks = explode("\n", $alllinks);

Open in new window

It appears to be designed to create an array of URLs.  Maybe it could be written like this:
$alllinks = array
( ''
, ''
, ''
, ''

Open in new window

All of those explode() and implode() statements do not add any value.

On line 52 we find this: "\n".  Like the short-tags issue, this creates an environmental dependency,  You might want to learn about the context-aware end of line constant, PHP_EOL.  It will make the code more portable.  This page is required reading for PHP programmers.

The code relies on variants of the deprecated ereg() functions.  You can either fix that or use an error reporting level with  (^DEPRECATED) to suppress the messages.  I would suggest you fix it - the fix is usually as simple as adding regex delimiters and flags for case-insensitivity.  Suppressing the messages is a little like putting a piece of black electrical tape over the "check engine" light on your dashboard.

That's as far as I got.  Without comments it's hard to follow the intent.  I usually write my comments first, then write my code.  Not only does this practice ensure that my code is well commented, it ensures that I can say in plain language what I want the program to do.  It's a tiny step in the overall process of consolidation of thought, but I have found that it serves me well to start with comments and follow with code, rather than the other way around.

HTH, ~Ray

Expert Comment

ID: 36502312
Ray I always enjoy reading your posts, they are so insightful. Quite often I learn a different way to do things in my own code based upon your insightful answers.

Thanks and to the OP he did a really good job of explain errors.  My original post intent was only about the length.  I agree with Ray that having no comments makes it difficult to figure out your code.

Author Comment

ID: 36504829
thanks so much, that was very helpful..

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP website on Linux - server DNS address could not be found. 18 87
if (is_singular not working 5 40
How to get this library to work load? 8 42
How Close unsubmited attempts 10 47
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question