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>​

6 Answers

4
Gordon On Best Solutions

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:

1
Oldskool On

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;​
1
Relequestual On

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.

1
Alex Angelico On

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>
1
umlcat On

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.

1
Bogdan On

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