[英]How to get span text in in php variable?
<span class="cid"></span>
In the above span, i am getting id from .js file.在上面的跨度中,我从 .js 文件中获取 id。 eg., 45 and i want that id in php variable.例如,45,我想在 php 变量中使用该 id。 eg., $cid='45';例如,$cid='45';
I have already tried below code, but i am unable to get id.我已经尝试过下面的代码,但我无法获得 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;
?>
Please try the below code and check at your end you can get class value in the array.请尝试下面的代码,并在最后检查您是否可以获得数组中的类值。
Second time Update code第二次更新代码
<?php
echo $cid='<span class="cid">45</span>';
?>
<?php
$str = $cid;
$DOM = new DOMDocument;
$DOM->loadHTML($str);
$items = $DOM->getElementsByTagName('span');
$span_list = '';
for($i = 0; $i < $items->length; $i++) {
$item = $items->item($i);
if($item->getAttribute('class') == 'cid'){
$span_list = $item->nodeValue;
}
}
echo $span_list;
?>
Code for multiple span tags and get single value from that span list array.多个跨度标签的代码并从该跨度列表数组中获取单个值。
<?php
$cid='<span class="cid">45</span> <span class="cid">48</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);
if($item->getAttribute('class') == 'cid'){
$span_list[] = $item->nodeValue;
}
}
//get the each value for multiple span tag
foreach ($span_list as $key => $value) {
echo $value;
echo '<br/>';
}
?>
try this one add span text in php code to get the span value in variable试试这个在 php 代码中添加跨度文本以获取变量中的跨度值
<?php $value = '<span class="cid"></span>'; ?> echo $value
Add an AJAX
code to pass it to PHP添加AJAX
代码以将其传递给 PHP
$.ajax({
url: 'your_php_file.php',
method: 'POST',
data: {cid: $('.cid')[0].innerText},
success: function(result){
console.log(result);
}
})
And in your PHP
file.并在您的PHP
文件中。 You can just use strip_tags
你可以只使用strip_tags
echo strip_tags($_POST['cid']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.