Reply
replace string within HTML
Old 05-06-2007, 02:23 AM replace string within HTML
Novice Talker

Posts: 9
Hi,

After searching through many sites and forums, we still have a difficulty with making str_replace to work for us. This obviously tells you we don’t know PHP for the very most of its part.

We are trying to replace an instance of “.html” to “.html?$something”

We successfully echo it, but it never goes down the code and does the action of replacement.

Ideally, the script would check the whole code between “body” tags of our HTML file and replace all of the instances found. It sounds so simple, yet headache for days.

Our code is like this:

Code:
 
<?php
$old = ".html";
$new = str_replace("html","html?$something",$old);
echo "$new";
?>
Echo shows what we want to be in place of all “.html” instances.

Why it doesn’t work?!

Thanks.
smallcompany is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 05-06-2007, 07:57 AM Re: replace string within HTML
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
Because you didnt understand what the single quote and double quote do in php

PHP Code:
$variable='something';
echo 
'$variable' // prints  $variable 
echo "$variable" // prints something -> the value of the varaible $variable 
Here is the code that will work
<?php
$old = ".html";
$something = "you're replace string";
$new = str_replace("html","html?$something",$old);
echo $new;
?>
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-06-2007, 02:29 PM Re: replace string within HTML
Novice Talker

Posts: 9
Thanks.

I don’t see how you used single quotes in your example. Nevertheless, I’ve already tried many combinations, and my problem is not getting the result shown by “echo”, that works.

My real problem is to get this script going down my HTML code and replace all instances of $old with $new.

The concept is:

You come to my site and load HTML page. During load, the script is being parsed. The script is to find all $old and replace it with $new before the page is shown on your screen.

Thanks.
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 02:42 PM Re: replace string within HTML
Mattmaul1992's Avatar
Ultra Talker

Posts: 465
Name: Matt
Well take a look. You're just adding something to the end of .html. So why make things complicated and replace html even though it is already there in the same place it would be in the end? Instead what you want to do is combine variables.
Here is how -

PHP Code:
$old ".html";
$something "page";
$new $old '?' $something;
echo 
$new
This combines old, a question mark, and something (change variable name to something a bit more descriptive) into new.
You can use your old way but it will take more CPU power and as you can already see probably give you errors. You both also forgot to add spaces after your ',' in your str_replace function. I'm not sure if this would cause an error because I always add a space but I can't imagine myself doing it any different besides with spaces (if I were to go along with str_replace) and I'm pretty sure it would work.
Next time when you get a headache over something for even a few hours don't worry to ask. =-) People like helping.
__________________
PHP Code:
$talkupation++; 
http://www.forum-front.com/ - Free IPB forum hosting (releasing today!!!), no ads, free modifications

Last edited by Mattmaul1992 : 05-06-2007 at 02:46 PM.
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 02:42 PM Re: replace string within HTML
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
what kind of pages do you have now ... HTML ?
basicly you can put you put you're html in a variable
$html='html code';
and then call the str_replace function with the echo
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-06-2007, 02:45 PM Re: replace string within HTML
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
mattmaul1992 ... read his post carrefully ... he doesnt add variables .. he replaces thing inside a HTML CODE
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-06-2007, 02:47 PM Re: replace string within HTML
Mattmaul1992's Avatar
Ultra Talker

Posts: 465
Name: Matt
Yes, If you go with str_replace that would save a few probably 1 byte (how much space is set aside for the char data type at least in C++ which is what PHP is based on) of memory making the server run faster when you have a good thousand requests going.
__________________
PHP Code:
$talkupation++; 
http://www.forum-front.com/ - Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 02:50 PM Re: replace string within HTML
Novice Talker

Posts: 9
All that work fine to echo it. If yo take the whole HTML code of any page within the site, you'll find bunch of internal links which all finish with ".html"

So... if somewhere in a middle of my code I have:

Code:
<p><a href="page1.html">Page 1</a></p>
<p><a href="page2.html">Page 2</a></p>


DOT HTMLs are not getting replaced no matter how I write my PHP. Why? I want to replace them all.
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 02:52 PM Re: replace string within HTML
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
Code:
$html='
<p><a href="page1.html">Page 1</a></p>
<p><a href="page2.html">Page 2</a></p>
';

$new = str_replace(".html",".html?page=testing", $htm);
echo $new;
try this one ... it should work
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog

Last edited by solomongaby : 05-06-2007 at 02:54 PM.
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-06-2007, 02:57 PM Re: replace string within HTML
Novice Talker

Posts: 9
How do you think I can do that on a site with 200+ pages that changes every day.

My goal is to automatically append a string to internal link so my variable gets carried over while you browse through the site.

Al what I am trying to do is to automate it. If I do it manually like “.html?$myvariable” it will work just fine.

I am trying to replace all “.html” with “.html?$myvariable” on page load. That way as long as I know that instance of “.html” is always my ineternal link, I have no worries as the code will be part of my template.

Why is this so hard?!
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 03:00 PM Re: replace string within HTML
Mattmaul1992's Avatar
Ultra Talker

Posts: 465
Name: Matt
You misnamed a variable there in the str_replace() function. Also sorry I didn't completely read the post XD. I'll make sure to start. :k. You also might want to do replaces for .php, because in order to use this page you need your extension to be .php unless you do a mod rewrite or edit php.ini so this way it'll work no matter the extension.
__________________
PHP Code:
$talkupation++; 
http://www.forum-front.com/ - Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 03:01 PM Re: replace string within HTML
Novice Talker

Posts: 9
My HTML is being parsed as PHP as I have other PHP stuff working OK.
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 03:03 PM Re: replace string within HTML
Novice Talker

Posts: 9
How do I make it going down the code and replacing all?

How do you state "all between body tags"?
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 03:06 PM Re: replace string within HTML
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
@smallcompany
1. What kind of files are you using HTML or PHP
because if it is HTML then the php code wont work

2. how does the website change .... you manualy change the code ?
3. How do you imagine doing this without editing the files ?
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-06-2007, 03:14 PM Re: replace string within HTML
Novice Talker

Posts: 9
1. HTML is being parsed as PHP just fine on Apache servers by simple setting in .httaccess so don't worry about that. PHP works 100%.
2. and 3. are not relevant to this challenge.

Take a look into this example:

Code:
HTML code starts…

….
<?php $string = myvariable;?>
…. text, images etc…

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

…some html code…

<p><a href="page1.html">Page 1</a></p>

….some code again…

HTML code finishes.



In the case of Page 1, myvariable will be appended. If I continue doing that with all links within all pages of my site, everything will work fine.

Now, how about saving some time and evading possibility of errors with simple str_replace or any other function that will replace or append or whatever as long as I get that done on the page load.

I have a feeling that if I can mark “all between body tags” that any of suggested solutions would work.
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-08-2007, 06:38 PM Re: replace string within HTML
Average Talker

Posts: 29
Name: Amit Soni
<?php
$old = ".html";
$new = str_replace("html","html?$something",$old);
echo "$new";
?>

The above doesn't seem to have a syntax problem.

It does have logical problems though.
  1. <html> </html> tags will get converted as well.
  2. What is contained in $something, might get misinterpreted as assignment of value to a variable/contstant instead of being treated as a string.
There has to be a silly mistake outside the lines that you posted here. post more code so we can attempt something.

--Amit.
Zitku is offline
Reply With Quote
View Public Profile Visit Zitku's homepage!
 
Old 05-08-2007, 11:19 PM Re: replace string within HTML
Novice Talker

Posts: 9
You’re right – syntax is OK. In regards of html tags, we can fix that by ‘.html’, or ‘.html>”’ (single quotes) or both.

In a meantime, there were some changes and some development has been made, and the current problem is in examples below:

Code that works, but DreamWeaver is not recognizing it anymore as the whole body is inside PHP tags:

Code:
<?php
$q = $_SERVER["QUERY_STRING"];
$code = '<body>
<p>&nbsp;</p>
<p><a href="page1.html">Page 1</a></p>
<p><a href="page2.html">Page 2</a></p>
<p>&nbsp;</p>
</body>';
$code = str_replace('html">','html?' . $q . '">',$code); 
echo $code;
?>
</html>
Code that is OK from DreamWeaver’s standpoint, but stuff between body tags is not getting parsed anymore:

Code:
<body>
<?php
$q = $_SERVER["QUERY_STRING"];
$code = str_replace('html">','html?' . $q . '">',$code); 
echo $code;
?>
<p>&nbsp;</p>
<p><a href="page1.html">Page 1</a></p>
<p><a href="page2.html">Page 2</a></p>
<p>&nbsp;</p>
</body>
</html>
How do we tell to that PHP script to go to the end of HTML code? How do you state “all between body tags” in PHP, if that is possible? Do we have to do some kind of “loop”, “callback”, something with curly brackets, whatever.

Another approach would be to call the script with php include, while the script would have, besides the existing code, some kind of call for file content, something like file_get_contents or file_something, anything that would parse the whole HTML code, from first to its last byte.

We hope this helps in understanding our problem.
smallcompany is offline
Reply With Quote
View Public Profile
 
Old 05-09-2007, 04:27 AM Re: replace string within HTML
Average Talker

Posts: 29
Name: Amit Soni
function get_page_body($page)
{
preg_match("/<body>(.*)<\/body>/imsU", $page, $matches);
return $matches[1];
}

//will return stuff between <body> and </body>. i.e if </body> is present.
Zitku is offline
Reply With Quote
View Public Profile Visit Zitku's homepage!
 
Old 05-09-2007, 04:46 PM Re: replace string within HTML
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
you coud use output buffering ... se here for details
http://www.php.net/manual/en/function.ob-start.php

the code would look something like
PHP Code:
 <?php

function callback($buffer
{
  
// replace all the apples with oranges
  
return (str_replace("apples""oranges"$buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush
();

?>
this will output

Quote:
<html>
<body>
<p>It's like comparing oranges to oranges.</p>
</body>
</html>
__________________
If you like my posts ... TK is appreci