Forum Moderators: coopster

Message Too Old, No Replies

all between the HTML tags

         

smallcompany

9:11 pm on May 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,

In PHP, how do you say "all between body tags", referring to HTML code?

i.e.

$body = “all code between tags <body> and </body>”

How do you write "all code between tags <body> and </body>" in PHP language so a script understands where to perform its task?

Thanks.

Little_G

11:37 pm on May 6, 2007 (gmt 0)

10+ Year Member



Hi,

You can use a regular expression:


<?php
$code = '<body> test test </body>';
preg_match('/<body>(.*)<\/body>/s',$code,$matches);
$body = $matches[1];
?>

Andrew

smallcompany

2:18 am on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks.

How would that look if I want to replace a string, multiple matches?

I played with str_replace but it would never work through the whole page.

Thanks.

Little_G

1:06 pm on May 7, 2007 (gmt 0)

10+ Year Member



Hi,

To replace thte contents of a tag, here's an example:


<?php
$code = '<a href="#">test1</a><a>test2</a>';
$code = preg_replace('/(<a[^>]*>)(.*?)(<\/a>)/is','$1w$3',$code);
?>

Andrew

smallcompany

3:24 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here is the code:

<body>
<?php $string = $_SERVER["QUERY_STRING"];?>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</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.

henry0

3:55 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

Little_G

3:56 pm on May 7, 2007 (gmt 0)

10+ Year Member



Hi,

<p><a href="page1.html?<?php echo $string?>">Page 1</a></p>
<p><a href="page2.html?<?php echo $string?>">Page 2</a></p>

What you've got already my be the best way to do it.

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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</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

smallcompany

4:24 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Little_G:


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.

Little_G

4:38 pm on May 7, 2007 (gmt 0)

10+ Year Member



Hi,

You can do it without preg using:

str_replace('html">','html?' . $q . '">',$code);

Andrew

smallcompany

5:18 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Interesting…

Preg_replace just did it 100% while str_replace gives blank page.

smallcompany

5:28 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry...

Both worked. I forgot echo for str_replace.

So all the time it was about putting the whole body of HTML inside PHP, rather than having everything inside body tags.

Since both str_replace and preg_replace give the same result, would str_replace be easier for server?

Thanks.

eelixduppy

5:34 pm on May 7, 2007 (gmt 0)



>> would str_replace be easier for server?

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.

smallcompany

5:50 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you. Will test it.

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…

eelixduppy

5:59 pm on May 7, 2007 (gmt 0)



If you use eval [php.net] on the string that has the php vars in it it should work. It will parse the string as php before you do anything else.

smallcompany

6:38 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since we've been busting our heads with this PHP lately, we've been visiting that site quite a bit. It is all but not for beginners. All examples there are good for folks that already know PHP. They are good for getting a concept of “how something works” but absolutely useless to people like I am.

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.

eelixduppy

6:56 pm on May 7, 2007 (gmt 0)



Sorry; slight misreading on my part :)

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;

smallcompany

7:29 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, now we go back to the beginning. We seem to be persistent to make this PHP automate stuff for us while it seems that PHP requires some echoing all the time which means lots of manual work.

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

smallcompany

7:36 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Got it to a degree... it spits out the page twice now. :)

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.

smallcompany

8:23 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Solved with:

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

henry0

10:03 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Positively great!
You try; you want to understand what's boiling down.
Another word you are on your way to learn by trial and learning from your errors and success rather than 100% borrowing internet stuffs ready to be grabbed :)

smallcompany

3:43 am on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks a lot.

And… thanks again to Little_G who brought this to the end for the most of its part.