tags:

views:

155

answers:

4

Okay so i set up this thing so that I can print out page that people came from, and then put dummy tags on certain pages. Some pages have commented out "linkto" tags with text in between them.

My problem is that some of my pages don't have "linkto" text. When I link to this page from there I want it to grab everything between "title" and "/title". How can I change the eregi so that if it turns up empty, it should then grab the title?

Here is what I have so far, I know I just need some kind of if/then but I'm a rank beginner. Thank you in advance for any help:

 <?php

 $filesource = $_SERVER['HTTP_REFERER'];
 $a = fopen($filesource,"r"); //fopen("html_file.html","r");
 $string = fread($a,1024);
 ?>
 <?php
 if (eregi("<linkto>(.*)</linkto>",
 $string, $out)) {
 $outdata = $out[1];
 }
 //echo $outdata;
 $outdatapart = explode( " " , $outdata);

 echo $part[0];

?>

+2  A: 

Here you go: if eregi() fails to match, the $outdata assignment will never happen as the if block will not be executed. If it matches, but there's nothing between the tags, $outdata will be assigned an empty string. In both cases, !$outdata will be true, so we can fallback to a second match on the title tag instead.

if(eregi("<linkto>(.*?)</linkto>", $string, $link_match)) {
    $outdata = $link_match[1];
}
if(!$outdata && eregi("<title>(.*?)</title>", $string, $title_match)) {
    $outdata = $title_match[1];
}

I also changed the (.*) in the match to (.*?). This means, don't be greedy. In the (.*) form, if you had $string set to

<title>Page Title</title>  ... 
  ... <iframe><title>A second title tag!</title></iframe>

The regex would match

Page Title</title> ... ... <iframe><title>A second title tag!

Because it tries to match as much as possible, as long as the text is between any and any other !. In the (.*?) form, the match does what you'd expect - it matches

Page Title

And stops as soon as it is able.

...

As an aside, this thing is an interesting scheme, but why do you need it? Pages can link to other pages and pass parameters via the query string:

<a href="somescript.php?prevpage=Foo">...</a>

Then somescript.php can access the prevpage parameter via the $_GET['prevpage'] superglobal variable.

Would that solve your problem?

rjh
I don't want ot change all the links because I was worried it would conflict with some other stuff that's being passed there. Thanks for your help, everyone. I knew how to do if/then but didn't know how to say "if no text matchint his description is there".
pg
A: 

you need if, else.

if(eregi(...))
{
    .
    .
    .
}
else
{
    just grab title;
}

perhaps you should have done a quick google search to find this very simple answer.

Samuel
Wow, he even said "I know it's some kind of if statement"......I agree a monkey could have googled this but you don't have to be so damn snarky.
patricksweeney
Yeah but where am I going to find a monkey?! I knew how to do "if" I just didn't know how to say "no text matches this". Thanks for your help though, I do appreciate it.
pg
A: 

Just add another if test before you assign the match to $outdata:

if (eregi("<linkto>(.*)</linkto>", $string, $out)) {
    if ($out[1] != "") {
        $outdata = $out[1];
    } else {
        // Look in the title.
    }
}
maxyfc
A: 

The POSIX regex extension (ereg etc.) will be deprecated as of PHP 5.3.0 and may be gone completely come PHP 6, you're better off using the PCRE functions (preg_match and friends).

The PCRE functions are also faster, binary safe and support more features like non-greedy matching etc.

Just a pointer.