Jump to content

PHP Current Page Help


jblack

Recommended Posts

Ok, so what I want to do is search through the currently opened page and replace any %tl% with <img src="images/tleft.gif">I can't seem to get this to work though... Here's my script right now as a test:

<html><head><style>body {background-color: black;margin: 0;}</style></head><?phpfor($i = 0; $i < 1; $i++) {str_replace("%tl%", "<img src='images/tleft.gif'>", $PHP_SELF);}?><body>%tl%

Link to comment
Share on other sites

I'm not entirely sure I know what you are trying to do, but I do see some errors that you can fix first and then go from there.Errors:1. You have your php code listed in between your </head> and <body> tags. Nothing should go here, your php should be in the body section of the page.2. Your loop is saying that i=0 and to run as long as i is less then 1 - how many times is this loop going to run?3. Your quotations are not properly closed on the str_replace line...... Recommendation - I would change the quotation marks in to look like this: str_replace('%tl%', '<img src="images/tleft.gif">", $PHP_SELF');Also, here is some str_replace() examples from php.net:

<?php// Provides: <body text='black'>$bodytag = str_replace("%body%", "black", "<body text='%body%'>");// Provides: Hll Wrld f PHP$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");// Provides: You should eat pizza, beer, and ice cream every day$phrase  = "You should eat fruits, vegetables, and fiber every day.";$healthy = array("fruits", "vegetables", "fiber");$yummy   = array("pizza", "beer", "ice cream");$newphrase = str_replace($healthy, $yummy, $phrase);// Use of the count parameter is available as of PHP 5.0.0$str = str_replace("ll", "", "good golly miss molly!", $count);echo $count; // 2// Order of replacement$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";$order   = array("\r\n", "\n", "\r");$replace = '<br />';// Processes \r\n's first so they aren't converted twice.$newstr = str_replace($order, $replace, $str);// Outputs: apearpearle pear$letters = array('a', 'p');$fruit   = array('apple', 'pear');$text    = 'a p';$output  = str_replace($letters, $fruit, $text);echo $output;?> 

Hope this helps! :)

Link to comment
Share on other sites

Ok, this is hard, I don't know where to start...Think I start with PHPJack's post:

1. You have your php code listed in between your </head> and <body> tags. Nothing should go here, your php should be in the body section of the page.2. Your loop is saying that i=0 and to run as long as i is less then 1 - how many times is this loop going to run?3. Your quotations are not properly closed on the str_replace line...... Recommendation - I would change the quotation marks in to look like this: str_replace('%tl%', '<img src="images/tleft.gif">", $PHP_SELF');
1. You CAN have php code anywhere on the page, between <head> and </head>, between <body> and </body>, between </head> and <body>, in the verry beginning of the document etc. 1st: php doesn't make an "impact" on the html that's send to the browser (unless you echo something out...), if you only were restricted to have PHP in the body you couldn't write verry advanced scripts. With that said, you should be carefull about what you output and how you output it, to make sure you output valid (x)html. You should also keep in mind how you structure your code and, altough it's valid, I would perhpaps not put that code there... (see more below)2. For those who wonder (!?) the loop would run one time so it's totally unnessary, just because it only run onces (if you remove it and only keep the content, the result would be the same) but also because you don't need the for-loop, it doesn't add any functionallity you need, str_replace replaces all occurrences (with or without loop).3. There's nothing wrong with the call to str_replace(), except that the output is not used (which make the call "unnessary" and has no effects). There's something wrong with your (PHPJack) example tough... You're right that it would be better/easier to use apostrophs (') insted of quots, but you include the third arguement in the string, which (among other things) make the call unvalid (as str_replace needs three arguments, and you only give it two). A correct line would be something like this:
echo str_replace( '%tl', '<img src="images/tleft.gif">', $PHP_SELF);

Ok, now on to the quetion:PHP_SELF (nomather how you call it) only contains the current filename (not the file itself), you can't modify any static content of the current file, you can't really modify the current file at all (you could, but not in the way you want).The thing you could (and should?) do is to have the HTML in a seperate file, then use a code like this in the "main file":

<?php// Get the html  (from the file templ.html)$html = file_get_contents( 'templ.hml' );// replace the "markers"$html = str_replace( '%tl%', '<img src="images/tleft.gif" />', $html );// Output the htmlecho $html;// Done :?)?>

That's (almost...) all you need ;?)I guess you want some sort of template and template engine, you could write it on your own (as you're trying) or use an existing (use gogle: php template engine).Another way to do it is to use php and echo:

<html><head><style>body {background-color: black;margin: 0;}</style></head><?php$tl = '<img src="images/tleft.gif" />';?><body><?php echo $tl; ?>

Good Luck and Don't Panic! ;?)

Link to comment
Share on other sites

I prefer the first way:

<?php// Get the html  (from the file templ.html)$html = file_get_contents( 'templ.hml' );// replace the "markers"$html = str_replace( '%tl%', '<img src="images/tleft.gif" />', $html );// Output the htmlecho $html;// Done :?)?>

But the question might get asked - how do you execute PHP code inside the template file? If it was actually a template file there wouldn't be any PHP code in it, but clearly jblack is not advanced enough at this point to take advantage of a full-blown template engine. I'm not trying to knock jblack's abilities, that's just what the situation is. I was the same way myself at one point.So, anyway, if you want to execute PHP code inside the template file, the way to do that is to include the file, capture the output, and then do the replace. That would look something like this:

<?php//enable output buffering to capture outputob_start();//include the file, capture outputinclude 'template.php';$output = ob_get_clean();// replace the "markers"$html = str_replace( '%tl%', '<img src="images/tleft.gif" />', $output );// Output the htmlecho $html;?>

That way you can have PHP code inside the file that will still get executed. You can even have PHP dynamically write out "%tl%" markers that will still end up getting replaced. Something like this is the very very beginning of what could turn into your own template engine if you add additional features (instead of just literal string replacement).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...