By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,350 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,350 IT Pros & Developers. It's quick & easy.

Pulling Title Tag out of HTML

P: n/a
Is there a simple piece of php code(s) that can pull the title of a
web page out? We need to pull the words in between <title></titleand
insert it into stumble upon. Here is what their requirements are:

http://www.stumbleupon.com/submit?ur...+Article+Title

We have the url but not the title.

The full instructions are here: http://www.stumbleupon.com/integrate.php

Any ideas?

Mar 2 '07 #1
Share this Question
Share on Google+
27 Replies


P: n/a
af**********@gmail.com wrote:
Is there a simple piece of php code(s) that can pull the title of a
web page out? We need to pull the words in between <title></titleand
insert it into stumble upon. Here is what their requirements are:

http://www.stumbleupon.com/submit?ur...+Article+Title

We have the url but not the title.

The full instructions are here: http://www.stumbleupon.com/integrate.php

Any ideas?
This should be easy with regular expressions. Try something like:

$text = file_get_contents($url);
preg_match('<title>(.*)</title>', $text, $matches);
$title = $matches[1];
Mar 2 '07 #2

P: n/a
Message-ID: <es**********@registered.motzarella.orgfrom Ulrich
Schmidt-Goertz contained the following:
>The full instructions are here: http://www.stumbleupon.com/integrate.php

Any ideas?

This should be easy with regular expressions. Try something like:

$text = file_get_contents($url);
preg_match('<title>(.*)</title>', $text, $matches);
$title = $matches[1];

And if you want your pages to validate make it

http://www.stumbleupon.com/submit?ur...+Article+Title
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 2 '07 #3

P: n/a
Thanks for all the replies but pretty newbie here. This is what I have
so far on the page:

<?php
$text = file_get_contents($url);
preg_match('<title>(.*)</title>', $text, $matches);
$title = $matches[1];
?>

How does one take the above and insert the variables into this URL?

http://www.stumbleupon.com/submit?ur...+Article+Title

Thanks for your patience.

Mar 2 '07 #4

P: n/a
Message-ID: <11*********************@p10g2000cwp.googlegroups. comfrom
af**********@gmail.com contained the following:
><?php
$text = file_get_contents($url);
preg_match('<title>(.*)</title>', $text, $matches);
$title = $matches[1];
?>

How does one take the above and insert the variables into this URL?

http://www.stumbleupon.com/submit?ur...+Article+Title

Thanks for your patience.
<?php
$filename=__FILE__;
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.$_SERVER['SCRIPT_URI'].'&amp;title='.urlencode($title);
?>

<a href='<?php echo $link;?>' Link to stumbleupon</a>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 2 '07 #5

P: n/a
ok, running this now:

<?php
$filename=__FILE__;
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.
$_SERVER['SCRIPT_URI'].'&amp;title='.urlencode($title);
?>

<a href='<?php echo $link;?>' Link to stumbleupon</a>
Unfortunately, I think because we are running the php in an include
file, the echo link is not working. Viewing the php file directly
works but when this piece of code is placed in a php file inside
a .shtml file, it no longer echos the url or title.

Any ideas of how we can get around this?

Mar 2 '07 #6

P: n/a
Message-ID: <11*********************@s48g2000cws.googlegroups. comfrom
af**********@gmail.com contained the following:
>Unfortunately, I think because we are running the php in an include
file, the echo link is not working. Viewing the php file directly
works but when this piece of code is placed in a php file inside
a .shtml file, it no longer echos the url or title.

Any ideas of how we can get around this?
Try this

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.$url.'&amp;title='.urlencode($title);
?>

<a href='<?php echo $link;?>' Link to stumbleupon</a>

Bear in mind that if your <title></titleis in an include this probably
won't work.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 2 '07 #7

P: n/a
af**********@gmail.com wrote:
ok, running this now:

<?php
$filename=__FILE__;
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.
$_SERVER['SCRIPT_URI'].'&amp;title='.urlencode($title);
?>

<a href='<?php echo $link;?>' Link to stumbleupon</a>
Unfortunately, I think because we are running the php in an include
file, the echo link is not working. Viewing the php file directly
works but when this piece of code is placed in a php file inside
a .shtml file, it no longer echos the url or title.

Any ideas of how we can get around this?
If your server is properly configured it should make no difference if
it's in an include file or not. The PHP processor should interpret any
..php file, whether called directly or included in another file.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 2 '07 #8

P: n/a
Jerry Stuckle wrote:
af**********@gmail.com wrote:
>ok, running this now:

<?php
$filename=__FILE__;
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.
$_SERVER['SCRIPT_URI'].'&amp;title='.urlencode($title);
?>

<a href='<?php echo $link;?>' Link to stumbleupon</a>
Unfortunately, I think because we are running the php in an include
file, the echo link is not working. Viewing the php file directly
works but when this piece of code is placed in a php file inside
a .shtml file, it no longer echos the url or title.

Any ideas of how we can get around this?

If your server is properly configured it should make no difference if
it's in an include file or not. The PHP processor should interpret any
.php file, whether called directly or included in another file.
OOps - lost the last part of my post.

But in you code I don't seen an echo statement. Is this the actual code
you're using?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 2 '07 #9

P: n/a
Message-ID: <Pa******************************@comcast.comfro m Jerry
Stuckle contained the following:
>If your server is properly configured it should make no difference if
it's in an include file or not. The PHP processor should interpret any
.php file, whether called directly or included in another file.

But __FILE__ will not give him what he wants.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 2 '07 #10

P: n/a
Ulrich Schmidt-Goertz wrote:
preg_match('<title>(.*)</title>', $text, $matches);
<title lang="en">My title</title>

;-)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 2 '07 #11

P: n/a
Rik
On Fri, 02 Mar 2007 18:46:54 +0100, Toby A Inkster
<us**********@tobyinkster.co.ukwrote:
Ulrich Schmidt-Goertz wrote:
>preg_match('<title>(.*)</title>', $text, $matches);

<title lang="en">My title</title>
Yup, [^>]* works wonders in HTML matching.

--
Rik Wasmus
Mar 2 '07 #12

P: n/a
Here is the error with the new codes running:

Warning: implode() [function.implode]: Bad arguments

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='removvedbeginningportion/submit?url='.
$url.'&title='.urlencode($title);
?>
<a href='<?php echo $link;?>'>Link to stumbleupon</a>

Jerry & Toby: Can you explain a little more?

Mar 2 '07 #13

P: n/a
Geoff Berrow wrote:
Message-ID: <Pa******************************@comcast.comfro m Jerry
Stuckle contained the following:
>If your server is properly configured it should make no difference if
it's in an include file or not. The PHP processor should interpret any
.php file, whether called directly or included in another file.


But __FILE__ will not give him what he wants.
Good point, Geoff - one that I missed.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 2 '07 #14

P: n/a
af**********@gmail.com wrote:
Here is the error with the new codes running:

Warning: implode() [function.implode]: Bad arguments

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='removvedbeginningportion/submit?url='.
$url.'&title='.urlencode($title);
?>
<a href='<?php echo $link;?>'>Link to stumbleupon</a>

Jerry & Toby: Can you explain a little more?
It usually means the second parameter isn't an array.

Try adding this before your implode:

echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";

This should print the file name followed by the file. Is the filename
correct, and if so, are you actually able to open (and display) it?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 2 '07 #15

P: n/a
Try adding this before your implode:
>
echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";
ok, added it and still got implode error. Commented out implode line
and got an echo to show, but no url or title. Here is what we have.
Any ideas?

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";
//$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link=removedtodisplayproperlyhere.com/submit?url='.
$url.'&title='.urlencode($title);
?>
<a href='<?php echo $link;?>'>Link to stumbleupon</a>

Mar 2 '07 #16

P: n/a
af**********@gmail.com wrote:
>Try adding this before your implode:

echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";

ok, added it and still got implode error. Commented out implode line
and got an echo to show, but no url or title. Here is what we have.
Any ideas?

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";
//$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link=removedtodisplayproperlyhere.com/submit?url='.
$url.'&title='.urlencode($title);
?>
<a href='<?php echo $link;?>'>Link to stumbleupon</a>
You got an echo to show - but was it the right information?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 2 '07 #17

P: n/a
You got an echo to show - but was it the right information?

The echoed link was missing the url & title content. Only the skeleton
link to SU. Anything else we can try here? Thanks for the help!

Mar 2 '07 #18

P: n/a
For what it's worth, I got this script to work at pulling the current
URL even if it is inside an include file. Still need the title though.

http://www.webcheatsheet.com/php/get...t_page_url.php

Mar 2 '07 #19

P: n/a
Rik wrote:
Yup, [^>]* works wonders in HTML matching.
<title foo=">" bar="quux">My Title</title>

;-)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 3 '07 #20

P: n/a
Rik
On Sat, 03 Mar 2007 00:46:27 +0100, Toby A Inkster
<us**********@tobyinkster.co.ukwrote:
Rik wrote:
>Yup, [^>]* works wonders in HTML matching.

<title foo=">" bar="quux">My Title</title>

;-)
Yeah, yeah, shouldn't be offcourse (foo="&gt;"), but hey: that's why you
normally parse html, not match it :-)

--
Rik Wasmus
Mar 3 '07 #21

P: n/a
af**********@gmail.com wrote:
>You got an echo to show - but was it the right information?

The echoed link was missing the url & title content. Only the skeleton
link to SU. Anything else we can try here? Thanks for the help!
Well, that explains why your implode fail then, doesn't it?

But specifically, what was in the echoed link? And what did you expect
to see?

I'm trying to help, but without the information I'm asking for, you make
it almost impossible to give you a good answer.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '07 #22

P: n/a
I'm trying to help, but without the information I'm asking for, you make
it almost impossible to give you a good answer.
Hey Jerry,

First of all, know that your help is really appreciated. Being a
newbie at coding, I am still trying to follow the lingo. Sorry about
missing some info. Let me try it here.

Running this:

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";
//$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='edited-so-url-displays://www.stumbleupon.com/submit?url='.
$url.'&title='.urlencode($title);
?>
<a href='<?php echo $link;?>'>Link to stumbleupon</a>

Resulting HTML is:

<a href='http://www.stumbleupon.com/submit?url=&title='>Link to
stumbleupon</a>

Want to get this:

<a href='http://www.stumbleupon.com/submit?url=http://www.mydomain.com/
file.html&title=Title of file.html'>Link to stumbleupon</a>

How was that?

Mar 3 '07 #23

P: n/a
Message-ID: <11**********************@v33g2000cwv.googlegroups .comfrom
af**********@gmail.com contained the following:
>Any ideas?
<shrug>It works on my machine

You have to learn to debug code. Work through it so you understand it
and echo the variables to make sure they contain what they should.
>
<?php
//assigns the script uri to the variable $url. echo it out to check
>$url=$_SERVER['SCRIPT_URI'];
//splits the uri into sections and creates an array of the pieces.
>$file=explode("/",$url);
//use print_r($file) to see what it contains

//get the last piece. This should be the file name or the page we are on
>$filename=$file[(count($file)-1)];
echo $filename . "<br>\n";
/you echoed it. Was it what it was supposed to be?
//If it's not the filename of the page we are on
//then that's why implode will fail
>echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";
//the next line gets the contents of the file and puts it
//into a string called $content so we can do the matching
//
>//$content = implode('', file($filename));
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 3 '07 #24

P: n/a
Message-ID: <ol********************************@4ax.comfrom Geoff
Berrow contained the following:
>
>>Any ideas?

<shrug>It works on my machine
Scratch that, it doesn't.
The variable $_SERVER['SCRIPT_URI'] was not available .

Try this

<?php
$url=$_SERVER['SCRIPT_FILENAME'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.$url.'&amp;title='.urlencode($title);
?>
<a href='<?php echo $link;?>' Link to stumbleupon</a>


Failing that try
$_SERVER['REQUEST_URI']
or
$_SERVER['SCRIPT_NAME']

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 3 '07 #25

P: n/a
af**********@gmail.com wrote:
>I'm trying to help, but without the information I'm asking for, you make
it almost impossible to give you a good answer.

Hey Jerry,

First of all, know that your help is really appreciated. Being a
newbie at coding, I am still trying to follow the lingo. Sorry about
missing some info. Let me try it here.

Running this:

<?php
$url=$_SERVER['SCRIPT_URI'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
echo $filename . "<br>\n";
echo "<pre>\n";
print_r(file($filename));
echo "</pre>\n";
//$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='edited-so-url-displays://www.stumbleupon.com/submit?url='.
$url.'&title='.urlencode($title);
?>
<a href='<?php echo $link;?>'>Link to stumbleupon</a>

Resulting HTML is:

<a href='http://www.stumbleupon.com/submit?url=&title='>Link to
stumbleupon</a>

Want to get this:

<a href='http://www.stumbleupon.com/submit?url=http://www.mydomain.com/
file.html&title=Title of file.html'>Link to stumbleupon</a>

How was that?
OK, so look at your code and what's different between what you get and
what you want?

Two things. First of all $url isn't displaying the correct info - so it
must be incorrect. What's in it? echo before your code and I suspect
you'll find it's empty. So maybe $_SERVER['SCRIPT_URI'] is incorrect,
Use print_r() to figure out what's in it.

The next problem is $title doesn't seem to contain the correct
information. But that's ok because your implode is commented out.

But notice that $filename in your implode() function call eventually
comes from $url, also.

Hmmm, two different problems, but both with the same source. What do
you want to bet $url doesn't have the information you want? echo it to
the display and see what it actually has.

BTW - I agree with Geoff. You need to develop some debugging
techniques. With some decent techniques you should be able to determine
the cause of your problem in 10-15 minutes, instead of a couple of days
trying to converse here on usenet.

That's not saying I won't continue to try to give you a hand here. But
you're going to need those techniques the more you program.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '07 #26

P: n/a
Geoff Berrow wrote:
Message-ID: <ol********************************@4ax.comfrom Geoff
Berrow contained the following:
>>Any ideas?
<shrug>It works on my machine

Scratch that, it doesn't.
The variable $_SERVER['SCRIPT_URI'] was not available .

Try this

<?php
$url=$_SERVER['SCRIPT_FILENAME'];
$file=explode("/",$url);
$filename=$file[(count($file)-1)];
$content = implode('', file($filename));
preg_match("/<title>(.*)<\/title>/i", $content, $match);
$title = $match[1];
$link='http://www.stumbleupon.com/submit?url='.$url.'&amp;title='.urlencode($title);
?>
<a href='<?php echo $link;?>' Link to stumbleupon</a>


Failing that try
$_SERVER['REQUEST_URI']
or
$_SERVER['SCRIPT_NAME']
It's available on my (Debian) Linux system with Apache 2.0.sumthun.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '07 #27

P: n/a
Message-ID: <pK******************************@comcast.comfro m Jerry
Stuckle contained the following:
>It's available on my (Debian) Linux system with Apache 2.0.sumthun.
Yeah, it was available on my webhost but not when I checked on my local
setup. Bit busy yesterday, couldn't remember where I'd tested it.

Incidentally, for the OP. To check what $_SERVER variables /are/
available do this:

foreach($_SERVER as $key=>$value){
echo"Key is: $key Value is:$value<br>";
}

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 3 '07 #28

This discussion thread is closed

Replies have been disabled for this discussion.