...

View Full Version : If / Replace



Tony M
12-20-2010, 04:45 PM
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 :thumbsup:

Fumigator
12-20-2010, 05:19 PM
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.

Tony M
12-21-2010, 01:37 PM
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)

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 :thumbsup:

Fumigator
12-21-2010, 08:29 PM
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?

tfburges
12-21-2010, 11:19 PM
$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.types.type-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

Tony M
12-22-2010, 02:10 PM
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:

He is 840iuiu
tony? 574kjk
asjkj22704hui
jkhjk45454gg
ghfgi55408jrdydy
hjhj3011gfgrx
jjjhjh2010jhjuyt

I tried to replace

$Text = 'He is 840iuiu....';
by

$Text = file('originalfile.php');
but it is not working...

tfburges
12-26-2010, 05:23 AM
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:


$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:

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum