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]; ?>
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.
I also changed the (.*) in the match to (.*?). This means, don’t be greedy. In the (.*) form, if you had $string set to
The regex would match
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
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:
Then somescript.php can access the prevpage parameter via the $_GET[‘prevpage’] superglobal variable.
Would that solve your problem?