phpnew Posted May 7, 2007 Share Posted May 7, 2007 Hello, Forum after forum, Googling all day, and yet no solution. We parse our HTML as PHP. All pages within the site are with “.html” extension. We would like to apply additional variable to all internal links within each page. Instead of maintaining manually, we would like to have a script that would automatically: append “?variable” to each internal link which would be “.html”+”?variable” or replace each instance of “.html” with “.html?variable” where variable is $variable. We played with str_replace quite a bit. It always worked but just to echo the result. It never went down the HTML code and replaced all instances of “.html” Why? Example: <?php $old = “.html”; $new = “.html?$variable” $result = str_replace($old, $new, $old); ?> $result will be what we want, it always worked, but how do we push it down the code until it gets to closing body tag </body> Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/ Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 you lost me but from what i can see .html?var will not work.. as a html file can't handle variable, anyways what do you mean by "but how do we push it down the code until it gets to closing body tag </body>" maybe regex is a better solution Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-246990 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 We just want to append our variable to the end of HTML link. Find all instances of ".html" and replace them with ".html?$variable" $variable is already in place from within another script. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-246994 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 ok, if you say so.. anyways what do you mean by "but how do we push it down the code until it gets to closing body tag </body>" maybe regex is a better solution Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-246996 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 <body> <?php $old = “.html”; $new = “.html?$variable” $result = str_replace($old, $new, $old); ?> <p> </p> <p> </p> <p><a href="page1.html">Page 1</a></p> <p><a href="page2.html">Page 2</a></p> </body> We would like all links enading with ".html" to be updated (replaced) with ".html?$variable" Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247008 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 you seam to not understand and jusr seam to repeat yourself, i have no time for this the only way your get what your trying to do to work is by getting php to parse the file and then echo to the screen, i think you need to learn a little more as the end result will still not work as you expect but good luck anyways Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247187 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks a lot for letting me know what I already knew: That I don’t know how to do it. If I have understanding of what you referred to, I would be helping people here. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247282 Share on other sites More sharing options...
neel_basu Posted May 7, 2007 Share Posted May 7, 2007 Sorry I didn't understand at all . I am Confused. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247480 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 No problem at all. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247483 Share on other sites More sharing options...
per1os Posted May 7, 2007 Share Posted May 7, 2007 Very confusing the way it was written out: <?php $old = ".html"; $new = ".html\?" . $variable; $result = str_replace($old, $new, $old); ?> You need to escape the ? as it can be used as a type of regex parameter. A side note you were also using magic quotes, it is good to use an editor that does not do magic quotes. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247514 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 <body> <?php $q = $_SERVER["QUERY_STRING"]; ?> <p> </p> <p><a href="links/links.php?m=link23">Outgoing Link 1</a></p> <p> </p> <p><a href="page1.html">Page 1</a></p> </body> Assuming that for a given session the variable $q=somekeyword, the result should be: <body> <p> </p> <p><a href="links/links.php?m=link23&myVariable=somekeyword">Outgoing Link 1</a></p> <p> </p> <p><a href="page1.html?somekeyword">Page 1</a></p> </body> Please note that the number of outgoing and internal links can be different for each page. For links of PHP type, the appended string should be "&myVariable=somekeyword" For links of HTML type, the appended string should be "?somekeyword" I hope this sheds some light to my initial non-understandable call for help. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247523 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2007 Share Posted May 7, 2007 If the links are in the same source file as the PHP code, this can't be done. What you need to do is have a PHP script open each HTML file, read the contents, do the replacement, write the changed contents, then execute the new file. Ken Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247532 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 Things are changing quickly here... </head> <?php $q = $_SERVER["QUERY_STRING"]; $code = '<body> <p><a href="links/links.php?m=link23999">Outgoing Link 1</a></p> <p><a href="links/links.php?m=link24999">Outgoing Link 2</a></p> <p><a href="page1.html">Page 1</a></p> <p><a href="page2.html">Page 2</a></p> </body>'; echo str_replace('html">','html?' . $q . '">',$code); echo str_replace('999">','&myVariable=' . $q . '">',$code); ?> </html> All works... except that page gets echoed twice. How do I do echo for both str_replace in one time? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247538 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2007 Share Posted May 7, 2007 You need to store the results in a variable and then echo the variable: <?php $code = str_replace('html">','html?' . $q . '">',$code); $code = str_replace('999">','&myVariable=' . $q . '">',$code); echo $code; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247542 Share on other sites More sharing options...
phpnew Posted May 7, 2007 Author Share Posted May 7, 2007 ... end everything works like a charm. Thank you so much to all that took time to read through this. We did not just solve our problem, but learned a lesson in regards of "how to post a question" Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/#findComment-247551 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.