-3

i have an h1, which i want to get the text from and then exacute it as an echo. for example something like this - http://jsfiddle.net/mZGH7/.

<html>
<head>
    <title><?php echo (".example"); ?></title>
</head>
<body>
    <h1 class="example">this is going to be the title</h1>
</body>
</html>​
mildog8
  • 2,030
  • 2
  • 22
  • 36
  • you want to put "this is going to be the title" into a variable, and then use that in . Use JQuery. –  Feb 11 '12 at 23:35
  • 1
    I'm sorry, I don't understand what you're trying to do. Please explain yourself better. – skimberk1 Feb 11 '12 at 23:35

6 Answers6

4

While you could do that with PHP's DOM extension and XPath:

<?php
    libxml_use_internal_errors(true);
    $dom = new DOMDocument;
    $dom->loadHTMLFile(__FILE__);
    $xpath = new DOMXPath($dom);
?>
<html>
<head>
    <title><?php echo $xpath->evaluate('string(//h1[@class="example"])'); ?></title>
</head>
<body>
    <h1 class="example">this is going to be the title</h1>
</body>
</html>

or with a third party lib implementing Selectors, like ZF's DOM Query component:

<?php
   libxml_use_internal_errors(true);
   $dom = new Zend_Dom_Query(file_get_contents(__FILE__));
?>
<html>
<head>
    <title><?php echo $dom->query('.example')->current()->nodeValue; ?></title>
</head>
<body>
    <h1 class="example">this is going to be the title</h1>
</body>
</html>

it is completely pointless to do it that way, because it takes much more time and is much more complicated than what you really should do, namely

<?php $title = 'this is going to be the title'; ?>
<html>
<head>
    <title><?php echo $title ?></title>
</head>
<body>
    <h1 class="example"><?php echo $title ?></h1>
</body>
</html>​

If that is not an option, use JavaScript and make sure to read:

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • While you are partly correct with your code, the first part of your answer is incorrect. It seems like they are trying to run PHP client side AFTER the HTML generation. – Relequestual Feb 11 '12 at 23:53
  • of course if it was a multi-phase php generation, that would work, but that isn't what we have here. – Relequestual Feb 11 '12 at 23:53
  • @Relequestual i dont see anything in the question that hints at that, though I agree that the OP is likely confused about client side and server side processing. – Gordon Feb 12 '12 at 00:02
  • 1
    Statements respectfully retracted =] Also now following you on twitter. – Relequestual Feb 12 '12 at 00:12
1

It appears to me as if you are trying to run PHP client side. PHP is server side. It generates the HTML and passes it to the browser. Once the HTML is generated, you then can use Javascript to manipulate the DOM. For that, you'll really want to use jQuery.

Relequestual
  • 11,631
  • 6
  • 47
  • 83
1

I think you are mixing php with javascript. Something like this works:

  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
</head>
<body>


    <h1 class="example">this is going to be the title</h1>

 <script>
 var salida = $(".example").html();
 $('title').html(salida);

 </script>
</body>
</html>
Alex Angelico
  • 3,710
  • 8
  • 31
  • 49
1

Quick Short answer

Create a html hidden field, can be accessed, both, by JavaScript, and PHP, at the same time.

Extended Boring answer

Developing a web page is different than developing a desktop application.

When a html page has controls, like text, radiobuttons, checkboxes. And the web page was generated dinamically using PHP. And the user sends them back to the server, the values can be accessed like variables in php.

There is something called "hidden field" that acts like a variable, or control, but, its contents, its not displayed, as it happens with a textbox control.

Remember that a webpage is executed 2 times, first, generated at the server, using PHP, HTML or another web language, and later, at the client's browser, with HTML, and JavaScript, if there is any.

Summary

You may want to learn about how html controls are generated and read back int PHP, and access with Javascript.

umlcat
  • 4,091
  • 3
  • 19
  • 29
1

as everyone else already said, it's not the best way to do it (using PHP, you should rather use javaScript if there aren't more complexities involved). that being said, PHP allows you to do it, by use of output control functions, like so :

<?php
function callback($buffer)
{ //using Gordon's answer ( http://stackoverflow.com/a/9245074/487891 )
  libxml_use_internal_errors(true);
  $dom = new DOMDocument;
  $dom->loadHTML($buffer);
  $xpath = new DOMXPath($dom);
  $buffer = str_replace("{{my_title}}",$xpath->evaluate('string(//h1[@class="example"])'),$buffer);
  return $buffer;
}

ob_start("callback");
?>

<!-- and then your html -->
<html>
<head>
    <title>{{my_title}}</title>
</head>
 <body>
   <h1 class="example">this is going to be the title</h1>
 </body>
</html>​

<?php  ob_flush(); ?>

i haven't tested this, so there might be errors in this code, but this would be the basic idea

Bogdan
  • 865
  • 6
  • 10
0

I can't think of a way to do that in PHP, but some some simple Javascript should do it. Something like this:

document.title = document.getElementsByTagName("h1")[0].innerHTML;​
Oldskool
  • 34,211
  • 7
  • 53
  • 66