Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP从HTML中删除一些文本_Php_Html_String - Fatal编程技术网

如何使用PHP从HTML中删除一些文本

如何使用PHP从HTML中删除一些文本,php,html,string,Php,Html,String,我将这样的HTML放入一个名为$HTML的PHP变量中: 我的网页 我的网页 {副标题} {BODY} 您可以使用正则表达式 您的正则表达式可以如下所示: {\w*} 这将搜索起始的。接着是{,1-n个文字或数字,}, $html=preg_replace('/{\w*}/',''$html); 这不仅会替换内容,还会删除标记本身。使用正则表达式充满问题,我会选择DOMDocument和DOMXPath $html=' <html> <head>

我将这样的HTML放入一个名为
$HTML
的PHP变量中:


我的网页
我的网页
{副标题}
{BODY}

您可以使用正则表达式

您的正则表达式可以如下所示:

{\w*}

这将搜索起始的
。接着是
{
,1-n个文字或数字,
}

$html=preg_replace('/{\w*}/',''$html);

这不仅会替换内容,还会删除标记本身。

使用正则表达式充满问题,我会选择
DOMDocument
DOMXPath

$html='
<html>
    <head>
        <title>MyPage</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>MyPage</h1>
        <h2>{SUBTITLE}</h2>
        <h2>Gigantic Ants ate my brain</h2>
        <div>{BODY}</div>
        <div>Squashed by a green banana</div>
    </body>
</html>';

$dom=new DOMDocument;
$dom->loadHTML( $html );
$xp=new DOMXPath( $dom );

/* tweak the XPath queries to suit your requirements */
/*
   There are many XPath cheatsheets available but for reference:
   http://xpath.alephzarro.com/content/cheatsheet.html
   http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
*/
$col=$xp->query('//h2[contains(text(),"{SUBTITLE}")]|//div[contains(text(),"{BODY}")]');

if( $col ){
    foreach( $col as $n )$n->parentNode->removeChild( $n );
}

echo '<textarea cols=100 rows=50>',$dom->saveHTML(),'</textarea>';
$dom=$xp=null;
$html='1!'
我的网页
我的网页
{副标题}
巨大的蚂蚁吃掉了我的大脑
{BODY}
被绿香蕉压扁
';
$dom=新的DOMDocument;
$dom->loadHTML($html);
$xp=新的DOMXPath($dom);
/*调整XPath查询以满足您的需求*/
/*
有许多XPath备忘单可供参考:
http://xpath.alephzarro.com/content/cheatsheet.html
http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
*/
$col=$xp->query('//h2[contains(text(),“{SUBTITLE}”)]|//div[contains(text(),“{BODY}”)]];
如果($col){
foreach($n列)$n->parentNode->removeChild($n);
}
echo“”,$dom->saveHTML(),“”;
$dom=$xp=null;

您可以使用正则表达式来匹配html标记之间的所有代码片段,并将其包装在
{}
中。在您的情况下,这将成为:

$html = '<html><head><title>MyPage</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><h1>MyPage</h1><h2>{SUBTITLE}</h2><div>{BODY}</div></body></html>';
$pattern = '/<([\w]+)[^>]*>{([^}]*)}<\/\1>/';

echo preg_replace($pattern, '', $html);
// outputs <html><head><title>MyPage</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><h1>MyPage</h1></body></html>
$html='MyPageMyPage{SUBTITLE}{BODY}';
$pattern='/]*>{([^}]*)}/';
echo preg_replace($pattern,,$html);
//输出MyPageMyPage

我建议在XPath方法中使用正则表达式:获取所有元素节点,这些节点的文本以
{
开头,以
}
结尾,带有
/^{[^}]+}$/
(正则表达式可以调整为
/^{[A-Z]+}$/
):

此行仅获取其内部文本(
text()
)与
$regex
匹配的所有元素(
*

这是一个:

$html=”“;
$dom=新的DOMDocument;
$dom->loadHTML($html,LIBXML\u html\u noimpled | LIBXML\u html\u NODEFDTD);//加载HTML字符串和初始化DOM
$xpath=new-DOMXPath($dom);//初始化XPATH
//您需要注册名称空间“php”以使其在查询中可用
$xpath->registerNamespace(“php”http://php.net/xpath");
$xpath->registerHPFunctions();
//在模式中添加分隔符
$regex='/^{[^}]+}$/';
//使用“/”在DOM树中的任意位置搜索节点
$items=$xpath->query(“/*[php:functionString('preg_match','$regex',text())>0]”);
foreach($tag形式的项目){
$tag->parentNode->removeChild($tag);
}
echo$dom->saveHTML();

使用
preg_replace()
代替带有regexp的
str_replace()
。我认为最好的方法是将XPath与正则表达式一起使用-请参见。这并不能回答这个问题,因为
{…}
之间的内容可以是任何内容。这个问题在内容上有点模糊,但对查询进行一些简单的调整可以产生所需的结果请参见,我认为这是这里最灵活的。我看到了-事实上我喜欢它,我非常喜欢它,因此+1短、有效且易于理解。谢谢:)谢谢,但是代码太多了,而只有一行代码:)
$html = '<html><head><title>MyPage</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><h1>MyPage</h1><h2>{SUBTITLE}</h2><div>{BODY}</div></body></html>';
$pattern = '/<([\w]+)[^>]*>{([^}]*)}<\/\1>/';

echo preg_replace($pattern, '', $html);
// outputs <html><head><title>MyPage</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><h1>MyPage</h1></body></html>
$xpath->query("//*[php:functionString('preg_match', '$regex', text())>0]")
$html = "<YOU_HTML_STRING_HERE>";
$dom = new DOMDocument;
$dom->loadHTML($html,  LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD); // Load the HTML  string and init DOM

$xpath = new DOMXPath($dom); // Init XPATH
// you need to register the namespace "php" to make it available in the query
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPhpFunctions();

// add delimiter to your pattern
$regex = '/^{[^}]+}$/';

// search your node anywhere in the DOM tree with "//"
$items = $xpath->query("//*[php:functionString('preg_match', '$regex', text())>0]");

foreach ($items as $tag) {
    $tag->parentNode->removeChild($tag);
}
echo $dom->saveHTML();