dousi4257 2011-09-13 09:15
浏览 46
已采纳

如何让PHP在显示页面之前操作HTML代码(复制/粘贴)

How to get PHP to manipulate HTML code (copy/paste) before sending to the browser.

I want to use PHP to get html code form inside h1 and paste it in h2. On server. Before page is send over to the browser.

e.g ;

<h1> Some HTML CODE </h1>
<h2> <!-- EMPTY --> </h2>

<?php 
   // $var1 = HTML CODE from inside of h1
   // Paste in $var1 inside h2
   // Display The Page
?>
  • 写回答

2条回答 默认 最新

  • drh47606 2011-09-13 09:17
    关注

    Edit: manipulating DOM before outputting through browser:

    <?php
    
    $my_html = '<div>
            <h1>This is a h1</h1>
            <h2>This is a h2</h2>
    </div>';
    
    $dom = new DOMDocument();
    $dom->loadHTML($my_html);
    
    $h1 = $dom->getElementsByTagName('h1')->item(0)->nodeValue;
    var_dump($h1); // "This is a h1"
    
    $dom->getElementsByTagName('h2')->item(0)->nodeValue = $h1;
    
    echo $dom->saveHTML();
    

    This is just an example. Read up on DOMDocument's documentation.


    Previous answer: You can't. PHP runs on the server side and javascript (thereby jQuery) on the client side. PHP doesn't know how to handle anything happening in the browser after the page has loaded.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站