Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/50313-solved-replace-string-within-html/
Share on other sites

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

<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"

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

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.

<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.

 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.