Page 2 of 2
Posted: Wed May 30, 2007 6:57 pm
by superdezign
That's because you're echoing the page inside of the same page. I'm not sure why your HTML can't just have the image tag inside of it.
Posted: Wed May 30, 2007 7:03 pm
by RobertGonzalez
So you have a PHP page that is embedded in HTML (or vice versa) and as it loads, you want the string '(pdf)' replaced with a link to something? Is this supposed to be dynamic? Can you use
include() or something of that nature?
Posted: Wed May 30, 2007 7:11 pm
by superdezign
You could use the output buffer to ensure that only one of the pages are shown but.... Eww.
This looks like a job for JavaScript!*
*Not an easy job... at all. :-p
Posted: Wed May 30, 2007 11:12 pm
by fullur
It is essentially intended to be dynamic. I already have dozens of pages with the (pdf) string used to denote .pdf files. I could go back and replace all of those with an image link (it wouldn't even be that much work using the "Find and Replace" feature), but I was thinking how much cooler and nicer if I could have a script that would take that string in any page as it was loading and replace it with the appropriate icon. It would also simplify things if I ever end up moving the icon as I would only have to change the address in one place. Then I could continue using the (pdf) string as I have been.
Posted: Thu May 31, 2007 7:25 am
by superdezign
fullur wrote:It is essentially intended to be dynamic. I already have dozens of pages with the (pdf) string used to denote .pdf files. I could go back and replace all of those with an image link (it wouldn't even be that much work using the "Find and Replace" feature), but I was thinking how much cooler and nicer if I could have a script that would take that string in any page as it was loading and replace it with the appropriate icon. It would also simplify things if I ever end up moving the icon as I would only have to change the address in one place. Then I could continue using the (pdf) string as I have been.
Okay then, output buffer it is.
Code: Select all
<?php
ob_start();
?>
<HTML>
.....
.....
</HTML>
<?
ob_end_clean();
$resource = file_get_contents("/usr/local/4admin/apache/vhosts/httpdocs/newsite/pdf_icon_script_test.php");
$resource = str_replace("(pdf)", "PDF_LINK", $resource);
print $resource;
?>
That's only if you absolutely have to have it print within the same page. This starts the output buffer, then cleans away the page. Then, you can replace (pdf) in the page, and print it out again with the (pdf) part replaced.
Posted: Thu May 31, 2007 11:35 am
by fullur
That worked for the string replacement. Unfortunately I use several
requires that I use in my site. Using this method they don't get displayed. I'm thinking that php just may not have what I am looking for here. Javascript might be better suited to this task. What do you think?[/url]
Posted: Thu May 31, 2007 11:47 am
by superdezign
Well, PHP doesn't have what you need to do it from within the file itself, that is pretty much correct. But.. we usually don't change files like that. When we make a file.... we make the file as it should be made.
What you're doing by replacing things is something I'd normally do for user input or something of that nature where users type in something simple and I convert it into something more complex, making their lives easier (bbCode comes to mind).
And yes, JavaScript could do it but it's not an simple task. I think that text nodes have a nodeName of #text, but you'd have to loop through all of the nodes considering that every node has a text node, which would most likely be done through a recursive function. Hardly efficient, and it will display (pdf) until the function is run later on in the file. But it is doable.
Posted: Thu May 31, 2007 2:15 pm
by fullur
So basically the task - while doable - would be inadvisable, correct? The more I look at it the more unreasonable it seems.
Posted: Thu May 31, 2007 2:28 pm
by superdezign
Right. Now, if you put all of your content into a database, loaded it from there, and then replaced (pdf), that'd sensible. However... I'm sure that's overkill.
Posted: Thu May 31, 2007 2:36 pm
by RobertGonzalez
It would seem that you may need to rethink what you are doing. Perhaps you could have a file called 'addition-pdf.txt' and in it you could have the string '(PDF)' (which I am still uncertain as to why you did this to begin with) and then include that file, or better,
file() that file, take its contents and
str_replace() what you want replaced with what you want to replace it with. Then you don't have the issues of trying to load a page and change that same page at the same time (which is still better handled client side using Javascript, but not as reliable as some people turn JS off).
Posted: Thu May 31, 2007 2:46 pm
by fullur
Perhaps you could have a file called 'addition-pdf.txt' and in it you could have the string '(PDF)' (which I am still uncertain as to why you did this to begin with)
The reason I did it is that I am developing a website and we have a lot of pdf files on it. According to what I have read, (and my own preference) the best practice for pdfs is to have them open in a seperate window and identify them. I had been doing so simply by adding (pdf) to the page immediately following the link, but then someone suggested using an icon instead. So that is where this whole project began. However, the whole thing just seems a bit impractical at this point and I have decided to just continue using text to identify the files.
Oh well, the whole thing hasn't been a waste. I may not be able to use what I learned on this, but I am sure I will run into something where it will be useful. Thank you for your assistance.
Posted: Thu May 31, 2007 2:47 pm
by superdezign
Why can't you just use the icon?
Posted: Thu May 31, 2007 2:48 pm
by RobertGonzalez
So you are literally trying to replace the text string '(pdf)' with an image of a pdf icon? In the markup? Why not ctrl-h in the project and be done with it?
Posted: Thu May 31, 2007 3:30 pm
by fullur
Why can't you just use the icon?
The fact is I can and probably will end up using the icon. What I meant was that I won't be trying any scripts to do it for me.
So you are literally trying to replace the text string '(pdf)' with an image of a pdf icon? In the markup? Why not ctrl-h in the project and be done with it?
Yep. That's exactly what I was trying to do. I am not familiar with the Ctrl-h shortcut but I am guessing that it's find and replace. That being said, I probably will.
Oh, and how do I change the name of this topic to make it clear it has been resolved?
Posted: Thu May 31, 2007 3:52 pm
by superdezign
Go to the first post, press edit, and add [Solved] to the title.