Inserting a portion of code from another page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edopizza
Forum Newbie
Posts: 1
Joined: Mon Apr 26, 2010 10:17 pm

Inserting a portion of code from another page

Post by edopizza »

Hello!
I am trying to solve a problem on how to insert a part of the code of an external web page into my php file. Tutorials handle this function with starting point and length, but because the external web page change frequently there is the need to use a string as point to mark the beginning and the end of the useful code section. Let's take an example with the google page.
"(...)" are omission to shorten the code display

Code: Select all

<!doctype html><html onmousemove="google&&google.fade&&google.fade(event)"><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><script> 
(...)
<div class=gbh style=left:0></div><div class=gbh style=right:0></div></div> <center><span id=body><center><br clear=all id=lgpd><img alt="Google" height=110 src="/intl/en_ALL/images/logo.gif" width=276 id=logo onload="window.lol&&lol()"><br><br><form action="/search" name=f onsubmit="if(document.f.action=='http://www.google.com/images'&&location.hostname!='images.google.com')document.f.action='/images'" onsubmit="google.fade=null"><table cellpadding=0 cellspacing=0><tr valign=top><td width=25%>&nbsp;</td>
(...)
ad",l,false);else if(window.attachEvent)window.attachEvent("onload",l);google.timers.load.t.prt=(f=(new Date).getTime());
})();
</script>
In this example the useful secton that need to be inserted into the php file is

Code: Select all

 
<img alt="Google" height=110 src="/intl/en_ALL/images/logo.gif" width=276 id=logo onload="window.lol&&lol()"><br>
Everything before

Code: Select all

 
...ter><span id=body><center><br clear=all id=lgpd>


and after

Code: Select all

 
<br><form action="/search" name=f onsubmit...


should be omitted. The end of the first omitted part, the beginning of the second omitted part and the beginning and the end of the useful part are unique and never change, so that they could be used to identify the entry and exit point.

Any idea where I can find a working example?
Thanks in advance!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Inserting a portion of code from another page

Post by Christopher »

You should check the PHP manual for the preg_match() function. There are examples there.
(#10850)
Post Reply