php中的html标签,javascript,php,html,wordpress,woocommerce"/>

首页 > 解决方案 > 如何从

php中的html标签

问题描述

我正在寻找如何<p id="test"></p>在 PHP 中获得数字。我有选择选项,并且使用下面的代码,我将我的变体 id 选为 html 段落 <p id="test"></p>,但我需要它作为 PHP 中的数字来运行另一个 PHP 代码来完成某些东西如何<p id="test"></p>在不使用另一个 java 脚本的情况下转换为 php 中的数字我只需要数字形式我下面的代码作为 php 中的变量号。

我在 WordPress 中使用 woocommerce 插件,我需要基于用户在单个产品页面中选择的选项来显示图像的新方法,而不是切换我需要用变体图像覆盖产品图像,因为我有用户选择的选项和下面的代码但我需要它在 php 中工作我不知道如何<p id="test"></p>将此代码转换为 php 以获取数字并做我知道如何做的其他事情我只需要<p id="test"></p>在 php 中获取数字。

 <?php

 if ( $product->is_type('variable') ) {

      ?>
      <script>
      jQuery(document).ready(function($) {

         $('input.variation_id').change( function(){
            if( '' != $('input.variation_id').val() ) {

               var var_id = $('input.variation_id').val();
               document.getElementById('test').textContent = var_id;
            }
         });

      });
      </script>
      <?php

   }

  ?> 

我需要<p id="test"></p>在 php 中获取而不是这个数字。

标签: javascriptphphtmlwordpresswoocommerce

解决方案


这个问题已经回答了

这里https://stackoverflow.com/a/3577662/10972786

&

这里https://stackoverflow.com/a/11240083/10972786

首先,确保服务器允许您获取数据。

其次,使用 html 解析器来解析数据。

// Gets the webpage
$html = @file_get_contents('https://yourwebsite.com/yourpage');

//if no webpage found die and return an error
if (!$html) {
  die('can not get the content!');
}

//Create a new DOM Doc
$doc = new DOMDocument();

//Load the HTML of the page you fetch in the DOM Doc
$doc->loadHTML($html);

//Get the element with the ID test
$content = $doc->getElementById('test');


请注意,如果元素为空,则 Dom 返回 NULL

不要为此使用javascript。

您可以在 PHP 文档中了解有关 DOMDocument 的更多信息:

https://www.php.net/manual/en/class.domdocument.php

这里有更多关于 getElementById

https://www.php.net/manual/en/domdocument.getelementbyid.php


推荐阅读