Reputation: 511
I need to display title from referring URL and here is the code I'm using to achieve that:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
$url_to_load = $_SERVER['HTTP_REFERER'];
$f = file_get_contents($url_to_load);
$p1 = strpos($f, "<title>");//position start
$qe = substr($f, $p1);//string from start position
$p2 = strpos($qe, "</title>");//position end
$query = substr($qe, 7, $p2-2);//cuts from start position +7 (<title>) untill end position -2...
echo $query;}
else{
$ref_url = 'No Reffering URL'; // show failure message
}//end else no referer set
echo "$ref_url";
?>
When i visit page with this code from URL that has the following code:
<title>Title Of Referrer</title>
Code works, but there is still the piece of the closing tag and when i check source code this is what i'll get:
Title Of Referrer</tit
What i need to change to remove the closing tag completely?
Upvotes: 2
Views: 219
Reputation: 23958
$query = substr($qe, 7, $p2-7);//cuts from start position +7 (<title>) untill end position -2...
You only subtract 2 at the end on end title but you add 7 on start title.
Try the code above and see if that works
EDIT:
Another solution is to do like this.
$query = strip_tags(substr($qe, 0, $p2));
This saves all of the title tags but then delete them with strip_tags()
EDIT2:
There are some other things in the code I would suggest.
$f = file_get_contents($url_to_load);
$query = strip_tags(substr($f, strpos($f, "<title>"), strpos($f, "</title>")));
This code brings it down to two lines of code and uses fewer variables. You can also get ridd of $f, but it may be useful to something else and it's only one variable.
Upvotes: 2