Replace text in html page at load [Solved]
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Okay then, output buffer it is.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.
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 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]
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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).
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.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)
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.Why can't you just use the icon?
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.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?
Oh, and how do I change the name of this topic to make it clear it has been resolved?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm