Php 来自<;标签>;

Php 来自<;标签>;,php,Php,我已经到处找了,我不知道这到底是怎么回事 我有标签 <taghere>content</taghere> 内容 我想提取“content”,这样我就可以根据“content”是什么放一个ifstatement,因为“content”根据页面的不同而变化 i、 e HelloWorld $content=//返回到之间的文本的函数 如果($content==“HelloWorld”) { //执行功能; } else if($content==“你好”) { //执行分

我已经到处找了,我不知道这到底是怎么回事

我有标签

<taghere>content</taghere>
内容
我想提取“content”,这样我就可以根据“content”是什么放一个ifstatement,因为“content”根据页面的不同而变化

i、 e

HelloWorld
$content=//返回到之间的文本的函数
如果($content==“HelloWorld”)
{
//执行功能;
}
else if($content==“你好”)
{
//执行分离函数
}

我尝试使用preg,但它似乎不起作用,只返回lines字段中的任何值,而不是实际提供标记中的信息

如果我正确理解您的问题,您希望标记“taghere”中的数据

如果您正在解析HTML,那么应该使用

尝试类似的方法:

<?php

// Assuming your content (the html where those tags are found) is available as $html

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your HTML
libxml_clear_errors();

// Note: Tag names are case sensitive

$text = $dom->getElementsByTagName('taghere');

// Echo the content
echo $text

我不知道您尝试了什么正则表达式,因此也不知道会出什么问题。可能是


我想只有一个你不能那样做。从技术上讲,这是可能的,但这不仅仅是一种过度的杀伤力。你把PHP和HTML混在了一起,这是行不通的

要实现您想要的目标,您必须执行以下操作:

$content = 'something';
if ($comtent === 'something') {
    //do something
} 
if ($content === 'something else') {
    //do something else
} 

echo '<tag>'. $content . '</tag>' ;
$content='something';
如果($comtent=='something'){
//做点什么
} 
如果($content==='something'){
//做点别的
} 
回显“”$内容",;

当然,您可以在ifs中更改
$content

您可以使用DomDocument和loadXML来实现这一点

<?php
function doAction($word=""){
$html="<taghere>$word</taghere>";
$doc = new DOMDocument();
$doc->loadXML($html);
 //discard white space 
 $hTwo= $doc->getElementsByTagName('taghere'); // here u use your desired tag 
if($hTwo->item(0)->nodeValue== "HelloWorld")
{
    echo "1";
}
else if($hTwo->item(0)->nodeValue== "Bonjour")
{
    echo "2";
    //execute seperate function
}
}
doAction($word="Bonjour");

别忘了,您可以随时将ID添加到标记中,以便使用java脚本引用它

<tag id='tagid'>blah blah blah </tag>

<script>
document.getElementById(tagid)
</script>
废话废话
document.getElementById(tagid)

这可能是一种更简单的方法来获取您的想法,然后是其他一些回答

您在哪里设置$content?抱歉,我添加了更多,但我的问题是-如何获取$content,这是两个阶段之间的内容,但这一内容在哪里?它是从xml提要读取的吗?从你正在爬行的网页中?啊,这将是每个页面页脚或页眉中的脚本,因此内容将从当前页面中读取。我倾向于提供答案,但考虑到这些信息,这是一个愚蠢的解决方案。它是你的服务器、你的控件、你的页眉,所以当你可以简单地设置和读取页眉/页脚文件中的变量时,没有很好的理由去做你所说的。我很难理解你怎么也不会事先知道这些是什么。我这里有个错误?[致命错误:在第47行的/public\u html/test.php中调用null上的成员函数getElementsByTagName()]我使用了basename($\u服务器['REQUEST\u URI']要获取当前文件名并将其放入$HTMLI,有一种方法可以使其成为一个函数,这样,如果HelloWorld是Bonjour而不是必须更改$html的值,它将回显2而不是1?尝试一下,这样做很好,我现在理解你了,我将按照你的意愿来做我将它作为一个函数来做,因此当你想更改它而不是更改变量时不可用的值您只需使用所需的单词作为参数调用函数
<?php
function doAction($word=""){
$html="<taghere>$word</taghere>";
$doc = new DOMDocument();
$doc->loadXML($html);
 //discard white space 
 $hTwo= $doc->getElementsByTagName('taghere'); // here u use your desired tag 
if($hTwo->item(0)->nodeValue== "HelloWorld")
{
    echo "1";
}
else if($hTwo->item(0)->nodeValue== "Bonjour")
{
    echo "2";
    //execute seperate function
}
}
doAction($word="Bonjour");
<tag id='tagid'>blah blah blah </tag>

<script>
document.getElementById(tagid)
</script>