如何在php变量中获取跨度文本?

<span class="cid"></span>

在上述范围内,我从 .js 文件中获取 id。例如,45,我想要 php 变量中的 id。例如,$cid='45';


我已经尝试过下面的代码,但我无法获得 id。


<?php echo $cid='<span class="cid"></span>'; ?>


<?php

    $str = $cid;


    $DOM = new DOMDocument;

    $DOM->loadHTML($str);


    $items = $DOM->getElementsByTagName('span');

    $span_list = array();


    for($i = 0; $i < $items->length; $i++) {

        $item = $items->item($i);

        $span_list[$item->getAttribute('class')] = $item->nodeValue;

    }

    extract($span_list);


    echo $cid; 

?>


LEATH
浏览 108回答 3
3回答

鸿蒙传说

试试这个在 php 代码中添加跨度文本以获取变量中的跨度值<?php$value = '<span class="cid"></span>';?>echo $value

红糖糍粑

请尝试以下代码并在最后检查您是否可以在数组中获取类值。第二次更新代码&nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; echo $cid='<span class="cid">45</span>';&nbsp;?><?php&nbsp; &nbsp; $str = $cid;&nbsp; &nbsp; $DOM = new DOMDocument;&nbsp; &nbsp; $DOM->loadHTML($str);&nbsp; &nbsp; $items = $DOM->getElementsByTagName('span');&nbsp; &nbsp; $span_list = '';&nbsp; &nbsp; for($i = 0; $i < $items->length; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $item = $items->item($i);&nbsp; &nbsp; &nbsp; &nbsp; if($item->getAttribute('class') == 'cid'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $span_list = $item->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; echo $span_list;?>&nbsp; &nbsp;多个跨度标签的代码并从该跨度列表数组中获取单个值。&nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; $cid='<span class="cid">45</span> <span class="cid">48</span>';&nbsp;?><?php&nbsp; &nbsp; $str = $cid;&nbsp; &nbsp; $DOM = new DOMDocument;&nbsp; &nbsp; $DOM->loadHTML($str);&nbsp; &nbsp; $items = $DOM->getElementsByTagName('span');&nbsp; &nbsp; $span_list = array();&nbsp; &nbsp; for($i = 0; $i < $items->length; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $item = $items->item($i);&nbsp; &nbsp; &nbsp; &nbsp; if($item->getAttribute('class') == 'cid'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $span_list[] = $item->nodeValue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //get the each value for multiple span tag&nbsp; &nbsp; foreach ($span_list as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; echo $value;&nbsp; &nbsp; &nbsp; &nbsp; echo '<br/>';&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }?>

肥皂起泡泡

添加AJAX代码以将其传递给 PHP$.ajax({&nbsp; &nbsp; url: 'your_php_file.php',&nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; data: {cid: $('.cid')[0].innerText},&nbsp; &nbsp; success: function(result){&nbsp; &nbsp; &nbsp; &nbsp; console.log(result);&nbsp; &nbsp; }})并在您的PHP文件中。你可以使用strip_tags&nbsp;echo strip_tags($_POST['cid']);
打开App,查看更多内容
随时随地看视频慕课网APP