Results 1 to 12 of 12
  1. #1
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello, is there any way to get an html title into a php variable? I have a form for users to send comments and I'd like to know which page the comment was sent from.

    Thanks,

    Chris

  2. #2
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post
    here is some air code

    Code:
    <?php
     $url = "http://ultramagicforest.blogspot.com/"; 
    $data = implode("", file($url)); 
    preg_match ("/<title>([^`]*?)<\/title>/", $data, $match);
     $urltitle = $match[1];  
    echo $urltitle; 
    ?>
    Jerry

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply. What is air code? I couldn't get it to work. This is what I have at the moment:

    Code:
    <?PHP
    /* This gets the title of the file and trims it to use in the email subject line*/
    $file = __FILE__;
    $a = fopen($file,"r");
    $string = fread($a,1024);
    if (eregi("<title>(.*)</title>", $string, $out)) {$pfile = $out[1];}
    ?>
    This works to get the title into the variable $pfile. The problem is that if I use it in an include it doesn't work. I'm looking for something I can put into an included file that will get the title of the parent file.

    Chris

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Cah View Post
    Hello, is there any way to get an html title into a php variable? I have a form for users to send comments and I'd like to know which page the comment was sent from.
    Can you store the title in a variable at the beginning of the page so you can populate it anywhere you like?

  5. #5
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscher2000 View Post
    Can you store the title in a variable at the beginning of the page so you can populate it anywhere you like?
    Thanks , but how? And also why? I want to find some code that works in an include so that I don't have to edit 300 plus files to add a variable by hand. I'm looking for a way to create the variable automatically.

    Chris

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Cah View Post
    Thanks , but how? And also why? I want to find some code that works in an include so that I don't have to edit 300 plus files to add a variable by hand. I'm looking for a way to create the variable automatically.
    Okay, I won't bother to answer the first two questions, in light of the sentences that follow.

    The magic constant __FILE__ is the rare case of PHP reporting the name of the included file. You can use either $_SERVER["SCRIPT_FILENAME"] or $_SERVER["SCRIPT_NAME"] to get the name of the including file.

    See attached example.

    [attachment=86799:PHP-file-names.zip]
    Attached Files Attached Files

  7. #7
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscher2000 View Post
    Okay, I won't bother to answer the first two questions, in light of the sentences that follow.

    The magic constant __FILE__ is the rare case of PHP reporting the name of the included file. You can use either $_SERVER["SCRIPT_FILENAME"] or $_SERVER["SCRIPT_NAME"] to get the name of the including file.

    See attached example.

    [attachment=86799:PHP-file-names.zip]
    Thanks for the code, it is interesting and useful but, unless I've misunderstood it, not what I'm looking for. Rather than the document path I want to be able to pull out the document title as defined inside the html code.

    Chris

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Cah View Post
    Rather than the document path I want to be able to pull out the document title as defined inside the html code.
    In your existing code, replace the __FILE__ constant with one of the other variables. Tweak until it works.

  9. #9
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Jefferson, I get what you mean now. I got confused by your kindness including the zip file. I didn't realise that __FILE__ only works with the included file. No wonder I was having trouble getting it to work with the parent!

    Chris

  10. #10
    New Lounger
    Join Date
    Dec 2009
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Cah View Post
    Thanks for the code, it is interesting and useful but, unless I've misunderstood it, not what I'm looking for. Rather than the document path I want to be able to pull out the document title as defined inside the html code.

    Chris
    There is a way to do what you want, but it's a bit fiddly. As others here have already suggested, use a $_SERVER variable to retrieve the full path and filename of the html file that contained the submitted form. You'll need to experiment to find out which server variable works, because it depends on how your particular server has been configured. Use:

    echo "<pre>";
    var_dump($_SERVER);
    echo "</pre>";

    to display all of the server variables, and look for the one that suits you best.

    Now, again in PHP, do a bit of clever string handling. Use the get_file_contents() function to read the entire content of that html file into a string. Search for <title>, using strpos to find its starting position. Search for </title> in the same way. You can then retrieve everything that occurs between those 2 positions and, presto, you'll have the page title.


    Rob Schifreen
    http://www.the-web-book.com (a free PHP/MySQL/HTML/CSS book that you can download)

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by rob schifreen View Post
    Use the get_file_contents() function to read the entire content of that html file into a string. Search for <title>, using strpos to find its starting position. Search for </title> in the same way. You can then retrieve everything that occurs between those 2 positions and, presto, you'll have the page title.
    Rob, are there advantages/disadvantages to using get_file_contents() vs. Chris's code example using fopen() and fread() (in post #805837)?

  12. #12
    New Lounger
    Join Date
    Dec 2009
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscher2000 View Post
    Rob, are there advantages/disadvantages to using get_file_contents() vs. Chris's code example using fopen() and fread() (in post #805837)?
    Either of these should work. However, web server operators often restrict your ability to open files, because of possible security risks, so it may be that one or other of them won't work on your particular host. Try it and see.

    And in future, write your HTML code "properly" and you won't have this problem. Eg, in the page head section,

    $page_title = "blah";
    echo "<title>" . $page_title . "</title>";

    to generate the page title. And then, in the <form> tag, include a hidden field containing the value of $page_title so that it gets passed to the page which retrieves the form's contents.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •