Forum Moderators: coopster
<body>
<?php $string = $_SERVER["QUERY_STRING"];?>
<p> </p>
<p> </p>
<p> </p>
<p><a href="page1.html">Page 1</a></p>
<p><a href="page2.html">Page 2</a></p>
</body>
With your script, how do you append $string to the end of each “html” link so they result in this form:
<p><a href="page1.html?<?php echo $string?>">Page 1</a></p>
<p><a href="page2.html?<?php echo $string?>">Page 2</a></p>
Basically, we want to append the $_SERVER["QUERY_STRING"] to the end of each HTML link, with “?”
We thought that some kind of PHP replace command would do it so each “html” within <a></a> tags gets replaced.
We know that, once this resolved, we could add some kind of control so other parts of the links do not get replaced.
<p><a href="page1.html?<?php echo $string?>">Page 1</a></p>
<p><a href="page2.html?<?php echo $string?>">Page 2</a></p>
If you use preg_replace then you need to have your HTML in a PHP string and echo it, also the regular expression engine uses quite a lot of resources.
If you still want to use it then:
<?php
$q = $_SERVER["QUERY_STRING"];
$code = '<body>
<p> </p>
<p> </p>
<p> </p>
<p><a href="page1.html">Page 1</a></p>
<p><a href="page2.html">Page 2</a></p>
</body>';
echo preg_replace('/(<a[^>]*href=")(.*?)("[^>]*>)(.*?)(<\/a>)/is','$1$2?' . $q . '$3$4$5',$code);
?>
Andrew
What you've got already my be the best way to do it.
This seem to be the cleanest way of doing it. The only problem is that we have to do it manually which brings in the possibility of error.
At the same time, we may end up doing simple find-replace in DreamWeaver for the whole site. It is bit of pain as this would force us doing it every time we add new page or do some changes.
That is why we thought about automating it. And yes, at the same time, we better be careful about server resources.
henry0:
If replace does not work alll the way down it is probably due to your loop ending early or your while not encompassing the whole area of interest
try moving " } " around
a simple replace looking for .html"> should work
with or without a concataination to recreate the new link
First, thanks for hitting the ball with
all the way down
All the time, we’ve been played with different “replace” functions and they would all work just to echo the result but never go down. That was probably because we are yet to learn how PHP works.
Based on your statement, would you know how to write the code from our post so we get that appended/replaced?
Thanks.
Should be less server intensive, although, I don't think anything noticeable. You can test it if you want using microtime [php.net]. There is an example in there that shows how to time scripts.
Now, still about inside the body tags.
PHP variables are not working anymore. This link, for example:
<p><a href="links/links.php?m=link24&myVariable=<?php echo $q?>">Outgoing Link 2</a></p>
In the past this link would work just fine with $q inserted there based on echo.
Now, since it's "inside" our PHP to be checked by str_replace function, it does not work anymore.
Ideally, the whole string
&myVariable=<?php echo $q?>
should get appended to the end of our PHP links in the same fashion as what you helped us do with internal “html” links.
The problem (for us) is that end of those links changes. It can be anything. The constant is:
links/links.php?m=
That is how we call all outgoing links since it is easier to maintain them in one PHP file.
So… some kind of str_replace checking on existence of the constant and appending “&myVariable=<?php echo $q?>” at the end would really make it 100% to work.
I feel cursed… and no PHP course in the city…
That is why most of us use FrontPage or DreamWeaver to create sites, rather than typing our HTML or PHP.
Please take this comment as directed to that reference site, not to your post which I am very thankful for.
The way how I see that eval() explained there is that I still cannot figure how I squeeze that into existing code, based on what Little_G provided me with, which was extremely helpful and resolved my initial query … then broke other part of the code.
For some reason PHP is always explained as within PHP. That may be a signal for us to get away from our HTML concept, not sure. I see many folks here are still in "HTML parsed as PHP" mode, which is exactly what we do now.
Oh my… time to buy a book and come back here in about 6 months (in regards of PHP). :)
Nothing happens overnight.
Try something like this. It gets rid of the str_replace completely:
$query = $_SERVER["QUERY_STRING"];
$html = "<p><a href=\"links/links.php?m=link24&$query\">Outgoing Link 2</a></p>";
echo $html;
Here is the current code, from ending head tag to the very bottom:
</head>
<?php
$q = $_SERVER["QUERY_STRING"];
$code = '<body>
<p><a href="links/links.php?m=link24&myVariable=<?php echo $p?>">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);
?>
</html>
Now, please pay attention to link24. “<?php echo $p?>">” is treated there as text, since it is inside variable $code, so it does not get parsed as PHP (that’s how I understand it).
How do we make that echo work? That would solve our problem while we would continue to manually maintain our outgoing links which means, every time we add new outgoing link, we have to remember to apply “&myVariable=<?php echo $p?> in order to get it done.
Better scenario would be like Little_G did it with “.html” links. Conceptually, it would be something like this:
Find string containing “links/links.php?m=*”>” and append “&myVariable=$p” to its end. Star* would mean everything between = and closing “>. In our example that would be link24.
That way we don’t care what is the value of “m” as we always apply our $q to the very end of it, but before closing ”>.
Here is the code:
</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>
How do I make this "double" str_replace to work as one?
Thank you.
$code = str_replace('html">','html?' . $q . '">',$code);
$code = str_replace('999">','&myVariable=' . $q . '">',$code);
echo $code;
Hey! Don't laugh over there... :)
Thank you so much.
We hope we'll be answering newbie’s queries like you do ours soon … or … relatively soon ;) ... so much in that PHP.