SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking for if file_get_contents is being pulled

    Hello trying to do this in PHP so search engines can search some of the items. I have an array for example:

    <?php

    PHP Code:

    $arr
    [1] = "http://www.boxofficemojo.com/imga/amazon/hancock_2disc.jpg"
    $arr[2] = "http://www.boxofficemojo.com/imga/amazon/hancock_3disc.jpg"
    $arr[3] = "http://www.google.com/index.html";
    $arr[4] = "http://www.testsite.com/1.txt"
    Then this, that is searching for certain kinds of images and certain kind of web site files from the array then to show one of them randomly:
    PHP Code:

    $img 
    preg_grep('/(?:jpg)|(?:png)/'$arr); 
    $fi preg_grep('/(?:html)|(?:txt)/'$arr); 
    $getimg $img[array_rand($img)]; 
    $files $fi[array_rand($fi)];
    $getfiles file_get_contents($fi);
    echo 
    $getimg;
    echo 
    $getfiles
    ?>

    How can I get it to echo a random web page file like .html or .txt for example only if it is pulling a file on that random pick, otherwise if its picking a random image on that pick, to echo the random image instead. Right now its echoing both a random file and a random image at the same time.

    I only need it to echo either an image or a file, whichever one it picks randomly on that pick.
    Seems like I need some sort of
    if {a random file_get_contents (if a random web page file is found)then do this... otherwise
    echo $getimg;} type statement of some kind, but don't know how to write it if it is pulling
    a file_get_contents on that pick. Please let me know how its done, thank you very much for your help.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Review the variables you are using...
    PHP Code:
    $files $fi[array_rand($fi)]; 
    $getfiles file_get_contents($fi); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already did and don't know how to take it further for it to show a file if it finds a file randomly, and if it doesn't then to show an image randomly. Please someone let me know, thank you very much for your help.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Think about it like this.

    $fi is an array
    $files is a string set to a random item in this array (incorrect code though - see below)

    So the file contents should be grabbed from $files, not $fi.

    PHP Code:
    $something $array[array_rand($array)]; 
    Is incorrect. array_rand returns the value not the key, so you would be using:
    PHP Code:
    $something array_rand($array); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I'm trying this then:

    PHP Code:
    <?php
    $arr
    [1] = "<img src='http://www.boxofficemojo.com/imga/amazon/hancock_2disc.jpg'/>"
    $arr[2] = "<img src='http://graphics.x10.com/images9/heart_pendant.jpg'/>"
    $arr[3] = "http://www.google.com/index.html"
    $arr[4] = "http://www.testsite.com/1.txt";  
    $img preg_grep('/(?:jpg)|(?:png)/'$arr); 
    $fi preg_grep('/(?:html)|(?:txt)/'$arr); 
    $getimg $img[array_rand($img)]; 
    $files $fi[array_rand($fi)];
    if (
    $files){$get file_get_contents($files); 
    echo 
    $get;}
    if (
    $getimg) { 
    echo 
    $getimg;}
    ?>
    Its showing the files and images but its still showing both at the same time, instead of which one it shows randomly at a time. I tried else and else if, didn't help. I thought that if I said "if ($files)" that would mean "if code returns a random web page file" then to show that file, otherwise only show the random image then instead. But its still showing both images and files at the same time. Please let me know how to have it show only one at a time based on which one it picks. Thank you very much for your help.

  6. #6
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a short amount of code. Anyone know how to help? Please let me know thank you very much for your help.

  7. #7
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    Thank you. I'm trying this then:

    PHP Code:
    <?php
    $arr
    [1] = "<img src='http://www.boxofficemojo.com/imga/amazon/hancock_2disc.jpg'/>"
    $arr[2] = "<img src='http://graphics.x10.com/images9/heart_pendant.jpg'/>"
    $arr[3] = "http://www.google.com/index.html"
    $arr[4] = "http://www.testsite.com/1.txt";  
    $img preg_grep('/(?:jpg)|(?:png)/'$arr); 
    $fi preg_grep('/(?:html)|(?:txt)/'$arr); 
    $getimg $img[array_rand($img)]; 
    $files $fi[array_rand($fi)];
    if (
    $files){$get file_get_contents($files); 
    echo 
    $get;}
    if (
    $getimg) { 
    echo 
    $getimg;}
    ?>
    Its showing the files and images but its still showing both at the same time, instead of which one it shows randomly at a time. I tried else and else if, didn't help. I thought that if I said "if ($files)" that would mean "if code returns a random web page file" then to show that file, otherwise only show the random image then instead. But its still showing both images and files at the same time. Please let me know how to have it show only one at a time based on which one it picks. Thank you very much for your help.
    This is what your code is doing right now in pseudo-code-english:

    Code:
    [SELECT RANDOM IMAGE]
    [SELECT RANDOM FILE]
    [DISPLAY RANDOM FILE]
    [DISPLAY RANDOM IMAGE]
    Are you wanting it to do this?

    Code:
    [SELECT RANDOM IMAGE OR FILE]
    [DISPLAY IT]
    Or like

    Code:
    [SELECT RANDOM IMAGE]
    [SELECT RANDOM FILE]
    [DISPLAY RANDOM BETWEEN THOSE TWO]
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  8. #8
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post

    PHP Code:
    $something $array[array_rand($array)]; 
    Is incorrect. array_rand returns the value not the key, so you would be using:
    PHP Code:
    $something array_rand($array); 
    No, he had it right, you have it backwards.

    array_rand() returns the key(s), NOT the value(s).

    Excerpt from php.net:

    Return Values

    If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.
    Last edited by bhanson; Nov 28, 2008 at 19:26. Reason: English =\
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  9. #9
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right now as you said its doing this:
    [SELECT RANDOM IMAGE]
    [SELECT RANDOM FILE]
    [DISPLAY RANDOM FILE]
    [DISPLAY RANDOM IMAGE]
    But I only want it to dislay which ever one it picks that time, instead of both at the same time.

    Any way to have it only display either an image or file randomly one at a time instead of both at the same time? Trying to keep preg grep so it can search for what I want. Please let me know, thank you very much for your help.

  10. #10
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    Right now as you said its doing this:
    But I only want it to dislay which ever one it picks that time, instead of both at the same time.
    You're not understanding what your code is doing.

    "which ever one it picks that time"

    Your code picks one of each type, and then displays both random values.

    Any way to have it only display either an image or file randomly one at a time instead of both at the same time?
    Yes, I provided two algorithmic solutions above, you need to decide how you want your program to work.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  11. #11
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much I did read your posts, you said:
    array_rand() returns the key(s), NOT the value(s).
    So why below there is this instead:
    $something = $array[array_rand($array)];
    this
    $something = array_rand($array);
    wasn't showing anything on the page. I explained real hard several times what am trying to do. With the following its showing everything well both a random image and a random file. But I only need it to show one end result not two at the same time. Hope someone can help me get past that last step. Thank you very much for your help.

    The PHP color coding wasn't working on this, this time.

    Code:
    <?php 
    $arr[1] = "<img src='http://www.boxofficemojo.com/imga/amazon/hancock_2disc.jpg'/>";
    $arr[2] = "<img src='http://graphics.x10.com/images9/heart_pendant.jpg'/>"; 
    $arr[3] = "http://www.google.com/index.html"; 
    $arr[4] = "http://www.testsite.com/1.txt";   
    $img = preg_grep('/(?:jpg)|(?:png)/', $arr); 
    $fi = preg_grep('/(?:html)|(?:txt)/', $arr); 
    $getimg = $img[array_rand($img)]; 
    $files = $fi[array_rand($fi)]; 
    if ($files){$get = file_get_contents($files); 
    echo $get;} 
    if ($getimg) { 
    echo $getimg;} ?>
    Last edited by 1Jen; Nov 28, 2008 at 23:40.

  12. #12
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    I explained real hard several times what am trying to do. With the following its showing everything well both a random image and a random file. But I only need it to show one end result not two at the same time.
    I know what you want your end result to be, but so far you have yet to define a non-ambiguous method for achieving your random result.

    There are multiple ways to get the same "apparent" result, but they're really very different.

    2 + 2 - 1 = 3
    1 + 1 + 1 = 3

    Those both equal 3 but are very different.

    Solutions:

    Code:
    [SELECT RANDOM IMAGE OR FILE]
    [DISPLAY IT]
    This selects a random image or file from the entire pool of images and files.

    Code:
    [SELECT RANDOM IMAGE]
    [SELECT RANDOM FILE]
    [DISPLAY RANDOM BETWEEN THOSE TWO]
    This selects a random image from the pool of images.
    Then selects a random image from the pool of files.
    Then randomly selects between image or file.

    They're different solutions with the same answer. They both provide a "random" answer but what type of "random" do you want?

    The first solution provides an evenly distributed random value across all elements, not discerning what types are which.

    The latter provides an evenly distributed random value across types.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  13. #13
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your help.

    [SELECT RANDOM IMAGE]
    [SELECT RANDOM FILE]
    [DISPLAY RANDOM BETWEEN THOSE TWO]
    There is a random image picked and then there is a random file picked. I didn't know it was possible to then pick randomly from those two results as well. If that is possible that would be all I would need. I have no idea how to then pick randomly from those two results though. Hope someone knows, thank you very much for your help.

    PHP Code:

    $getimg 
    $img[array_rand($img)]; 
    $files $fi[array_rand($fi)]; 
    $getfiles file_get_contents($files); 
    $get array_rand($getimg $getfiles)????... mt_rand()?
    echo 
    $get

  14. #14
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are both solutions, hopefully you can see more clearly the difference between the two.

    Code:
    [SELECT RANDOM IMAGE]
    [SELECT RANDOM FILE]
    [DISPLAY RANDOM BETWEEN THOSE TWO]
    PHP Code:
    $img preg_grep('/(?:jpg)|(?:png)/'$arr);
    $fi preg_grep('/(?:html)|(?:txt)/'$arr);
    $getimg $img[array_rand($img)]; // select random image
    $files $fi[array_rand($fi)];    // select random file

    $rand rand(01); // choose between the two
    if (== $rand) {
        
    $get file_get_contents($files); 
        echo 
    $get;
    } else {
        echo 
    $getimg;


    Code:
    [SELECT RANDOM IMAGE OR FILE]
    [DISPLAY IT]
    PHP Code:
    $randItem $arr[array_rand($arr)]; //select random image or file
    if (== preg_match('/(?:jpg)|(?:png)/'$randItem)) {
        echo 
    $randItem;
    } else if (
    == preg_match('/(?:html)|(?:txt)/'$randItem)) {
        
    $get file_get_contents($randItem);
        echo 
    $get;
    } else {
        throw new 
    Exception('Invalid type.');

    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  15. #15
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot, when done that way the second one is the best way. I sent you something.
    Wondering what does the 1 do in these?
    if (1 == preg_match(
    Thank you very much for your help.

  16. #16
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    Thanks a lot, when done that way the second one is the best way. I sent you something.
    Wondering what does the 1 do in these?
    if (1 == preg_match(
    Thank you very much for your help.
    preg_match() returns the number of times it matches the PATTERN. It will always be 0 or 1.

    0 if it didn't match anything
    1 if it found a match

    Therefore when we check to see if (1 == preg_match), we're checking to see if it found anything.

    So if you wanted to check to see if it didn't match, just do (0 == preg_match)

    http://php.net/preg_match

    There's also preg_match_all() which will not stop at the first match, and will try to find them all.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  17. #17
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    array_rand() returns the key(s), NOT the value(s).
    Strange, I seem to remember it being the other way round...

    Therefore when we check to see if (1 == preg_match), we're checking to see if it found anything.

    So if you wanted to check to see if it didn't match, just do (0 == preg_match)
    The typical way of doing this would be to check if preg_match == 1, because thats how logic works in our minds (well, mine - I'm assuming the same with most people).
    Of course it makes no difference whatsoever.

    What I would recommend, however, is using something like:
    PHP Code:
    <?php
    function endsWith($string$end){
        if(
    is_array($end)){
            foreach(
    $end as $item){
                if(
    endsWith($string$item)) return true;
            }
            return 
    false;
        }else{
            return (
    substr($string, -strlen($end)) == $end);
        }
    }if(
    endsWith($randItem, array('jpg''png'))){
        echo 
    $randItem;
    }else if(
    endswith($randItem, array('html','txt'))){
        
    $get file_get_contents($randItem);
        echo 
    $get;
    } else {
        throw new 
    Exception('Invalid type.');
    }
    That way a) you aren't wasting processing on regex, but most importantly a file like dangerousfilepng.exe wouldn't get through.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  18. #18
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    The typical way of doing this would be to check if preg_match == 1, because thats how logic works in our minds (well, mine - I'm assuming the same with most people).
    Of course it makes no difference whatsoever.
    The methodology behind placing constants first in comparisons is to provide a faster debug cycle when you screw up.

    Compare

    Code:
    if (preg_match() = 1)
    
    Fatal error: Can't use function return value in write context
    to

    Code:
    if (1 = preg_match())
    
    Parse error: syntax error, unexpected '='
    Or even more nasty:

    Code:
    if ($someVar = 1)
    
    Valid code, no error, but most likely not intended
    vs

    Code:
    if (1 = $someVar)
    
    Parse error: syntax error, unexpected '='
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  19. #19
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    errored
    Last edited by 1Jen; Nov 29, 2008 at 22:22.

  20. #20
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    Thank you, do you think there is a problem with this then

    since the constant isn't first?
    This one $rand = rand(0, 1); still blows my mind how it knows to look through array when array name isn't even stated. You guys know your codes.
    1 is the constant, preg_match() returns the variable result.

    rand() is just another function, just like preg_match(), or file_get_contents()
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  21. #21
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is very funny in a real unfunny way, means I've been trying to do it the harder way. I wasn't getting that there are a lot of pretty much already premade functions. Thanks a lot bhanson you're great.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •