Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: If / Replace

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    If / Replace

    I have a php page that on it I have numbers (that are automatically re-created every day....)
    I do not want to edit this main file.
    I want a code (on a separate page) that IF it found the following numbers (2010 or 2011 or 3010 or 3011 (every number must have it's own condition)) and in specific lines (lines 7 and 20 and 32) it will generate a message that I previously enter and if it do not found a number I will generate a message. (and the messages will be in a new page)

    So How can I do that?

    Any Suggestion Will Be Helpful

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Do you have access to these number when they are generated? Can you branch from that point to generate this message you mentioned? Or are you looking at someone else's webpage with the numbers on it you need? For that scenario you need to scrape that webpage. Google "php scrape" for tons of information how to do this.

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    ok, I have this page (that the words in it change every day) every day that is on My website (and on My website server)
    Code:
    He is 840iuiu 
    tony? 574kjk
    asjkj22704hui
    jkhjk45454gg
    jjjhjh2010jhjuyt
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jhjh54545mnbmy
    I want a code that read the code above and re-create a new file, the rules are:
    If 2010 is found on line 4 I get "Tony Is Here" else "Tony is not here"
    If 2011 is found on line 4 I get "Tony Is back" else "Tony is not here"

    and also

    If 3010 is found on line 6 I get "Tony Is Here 2" else "Tony is not here 2"
    If 3011 is found on line 6 I get "Tony Is back 2" else "Tony is not here 2"

    Any suggestion will be helpful
    Last edited by Tony M; 12-21-2010 at 01:40 PM.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If this is your own website then I assume you can make these determinations within the code that creates the webpage and act accordingly. Yes? No?

  • #5
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    PHP Code:
        $Text 'He is 840iuiu 
    tony? 574kjk
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy'
    ;
        
    $Lines explode(PHP_EOL$Text);  // Splits the lines up into an array
        // The array's index starts at 0 (zero) so...
        // Line 7 is $Lines[6], Line 20 is $Lines[19], and Line 32 is $Lines[31]

        // To quickly set up the parameters you want to follow, I'd use a multidimensional array like this:
        
    $Params = array(
            
    '6' => array(
                
    '2010' => array(
                    
    'Some text for when "2010" is NOT found on line 7...',
                    
    'Some text for when "2010" is found on line 7...',
                )
            ),
            
    '19' => array(
                
    '2011' => array(
                    
    'Some text for when "2011" is NOT found on line 20...',
                    
    'Some text for when "2011" is found on line 20...'
                
    )
            ),
            
    '31' => array(
                
    '3010' => array(
                    
    'Some text for when "3010" is NOT found on line 32...',
                    
    'Some text for when "3010" is found on line 32...'
                
    )
            )
        );

        
    // And then set up a couple of for loops with if statements to handle the parameters
        
    foreach ($Params as $LineNum => $Find) {
            if (isset(
    $Lines[$LineNum])) {
                foreach (
    $Find as $SubText => $Message) {
                    
    // Up above you see that I didn't assign a key to the array value in $Params, and put the "NOT found" message first...
                    // It's because I want the "NOT found" message to have the index of 0
                    // And the "found" message to have the index of 1... so that we can quickly do this:
                    
    $Found = (int)(substr_count($Lines[$LineNum], $SubText) > 0);    // if found, a "1" is assigned; if not, a "0" is assigned
                    
    $Messages[] = $Message[$Found];  // And now we add the proper message to messages
                    // (You could have used a bool comparator on strpos instead of substr_count but it's not a big deal at all...)
                
    }
            }
        }
        
    $Filename 'somefilename.txt';
        
    $Data = isset($Messages) ? implode(PHP_EOL$Messages) : 'Something went wrong!';
        
    file_put_contents($Filename$Data); 
    What's good about the multidimensional array approach is that you can quickly and easily (and efficiently) add/remove all kinds of messages and parameters that you need.

    Key PHP concepts/functions you should know:
    http://www.google.com/search?q=PHP_EOL
    http://php.net/manual/en/function.explode.php
    http://php.net/manual/en/language.types.array.php
    http://php.net/manual/en/control-structures.foreach.php
    http://php.net/manual/en/function.isset.php
    http://php.net/manual/en/language.ty...e-juggling.php (more specifically, type casting)
    http://php.net/manual/en/function.substr-count.php
    http://php.net/manual/en/function.implode.php
    http://www.google.com/search?q=php+shorthand
    http://php.net/manual/en/function.file-put-contents.php
    Last edited by tfburges; 12-21-2010 at 11:32 PM.

  • Users who have thanked tfburges for this post:

    Tony M (12-30-2010)

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tfburges View Post
    What's good about the multidimensional array approach is that you can quickly and easily (and efficiently) add/remove all kinds of messages and parameters that you need.
    Ok, but the text automatically change every day, so how can I input automatically the original file and not I enter the numbers manually in this php file?
    (input the text automatically form another file) I am talking about the text of:
    PHP Code:
    He is 840iuiu 
    tony
    574kjk
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt 
    I tried to replace
    PHP Code:
    $Text 'He is 840iuiu....'
    by
    PHP Code:
    $Text file('originalfile.php'); 
    but it is not working...
    Last edited by Tony M; 12-22-2010 at 02:15 PM.

  • #7
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    Do the line numbers and/or their corresponding messages change? I was assuming the text you were looking at was the only thing that changes.... in which case, you just need to do this:

    PHP Code:
    $Text file_get_contents('/enterthecorrectpath/textfile.txt'); 
    http://php.net/manual/en/function.file-get-contents.php

    And I'm assuming the text file (textfile.txt) looks something like this:
    Code:
    He is 840iuiu 
    tony? 574kjk
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    asjkj22704hui
    jkhjk45454gg
    ghfgi55408jrdydy
    hjhj3011gfgrx
    jjjhjh2010jhjuyt
    jhjh54545mnbmy
    But from your last post it looks like for some reason you're dynamically generating a PHP file? ...Is this true? You shouldn't need to do that. I mean you could, and it may actually be even more efficient (if generated in PHP array format to cut out the explode portion of splitting up the lines)... but I guess it's just a matter of the level of control you have over the generation of the file.

    If the numbers and/or their corresponding messages also change... you could do something similar and dynamically generate the multidimensional parameter array (using variables for the line numbers, search strings, and messages), depending on how/where those parameters are being generated. I can help you further with more details on this.
    Last edited by tfburges; 12-26-2010 at 05:33 AM.


  •  

    Posting Permissions

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