how to filter/search/replace on a page

M

mentalguy2004

Hi,

I want to be able to fetch a 3rd party page to my server, search for
multiple (different) specific text strings and replace them with my own,
then output the changed page to my own server. Is there anything out there
that can help me do this without having to write lots of code? Something
like those "joke" filters where you paste a URL into it and it loads that
page with silly words & "graffiti" on it although I only need to replace
specific words with other specific words, nothing random.

Currently, I'm having to download the 3rd party page source, paste it into a
search& replace Windows prog with several filters, copy the result into an
HTML page and upload it. I was looking for something that could do the job
*slightly* quicker.... my server can use PHP, ASP, CGI & HTML.

Thanks.
 
A

Andrew Urquhart

*mentalguy2004* said:
I want to be able to fetch a 3rd party page to my server, search for
multiple (different) specific text strings and replace them with my
own, then output the changed page to my own server. Is there anything
out there that can help me do this without having to write lots of
code? Something like those "joke" filters where you paste a URL into
it and it loads that page with silly words & "graffiti" on it
although I only need to replace specific words with other specific
words, nothing random.

Currently, I'm having to download the 3rd party page source, paste it
into a search& replace Windows prog with several filters, copy the
result into an HTML page and upload it. I was looking for something
that could do the job *slightly* quicker.... my server can use PHP,
ASP, CGI & HTML.

An ASP/IIS solution would be to use 'ServerXMLHTTP' to fetch the
requested document and then use regular expressions on the returned
'responseText' to do your replacement. I suggest that you do this in a
separate back-end process (ie. not when a user makes a request for your
version of the page) and cache the result if the fetch was successful.
End-users are then piped this cached version (perhaps via
Server.Execute) when they make a request for the page.

Additionally if the source server supports 'Etag' and 'last-modified'
headers you can use the 'If-Modified-Since' and 'If-None-Match' headers
in your request to see if the document has changed since you last asked
for it and so potentially save on server resources.
--
Andrew Urquhart
- FAQ: http://www.html-faq.com
- Archive: http://www.google.com/groups?q=alt.html
- Contact me: http://andrewu.co.uk/contact/
- This post is probably time-stamped +1 hour - blame my ISP (NTL)
 
T

Toby Inkster

mentalguy2004 said:
I want to be able to fetch a 3rd party page to my server, search for
multiple (different) specific text strings and replace them with my own,
then output the changed page to my own server.

In PHP something like this ought to work:

<?php
$content = file_get_contents("http://example.org/somefile.html");
$newcontent = preg_replace('/foo/', 'bar', $content);
print $newcontent;
?>
 
T

Toby Inkster

mentalguy2004 said:
OK, I've got that working, but I can't for the life of me get it to process
multiple replacements at the same time.

<?php
$content = file_get_contents("http://example.org/somefile.html");
$newcontent = preg_replace('/Thomas/', 'Tom', $content);
$newcontent = preg_replace('/Richard/', 'Dick', $newcontent);
$newcontent = preg_replace('/Harold/', 'Harry', $newcontent);
print $newcontent;
?>
 
M

mentalguy2004

OK, I've got that working, but I can't for the life of me get it to process
multiple replacements at the same time.

I've tried most ways, but am having to put a different version of the line
$newcontent = preg_replace('/foo/', 'bar', $content);

for every different string, and it's ignoring all instances of each. The
original page is fetched and displayed fine, but there are no changes to it.

How can I insert the different strings to search for and replace into this
bit of code?

thanks for your help~!
 
T

Toby Inkster

mentalguy2004 said:
I've got it working perfectly, thanks! Just one final question, how do I get
the "Tom" "Dick" or "Harry" to be a different colour to the rest of the
text?

preg_replace('/Thomas/','<strong style="color:green">Tom</strong>',$content);
 
M

mentalguy2004

I've got it working perfectly, thanks! Just one final question, how do I get
the "Tom" "Dick" or "Harry" to be a different colour to the rest of the
text? I need to highlight them, but I don't know how to do this. Thanks a
lot :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top