-3
我想获取html标签的<title>...</title>
中的内容。我使用了下面的代码,但它不起作用。为什么这个正则表达式不起作用?
<?php
$content = "<title>";
preg_match_all("/<title>/",$content,$title);
echo $title[0][0];
?>
如何在php中获取<title>...</title>
中的内容。
我想获取html标签的<title>...</title>
中的内容。我使用了下面的代码,但它不起作用。为什么这个正则表达式不起作用?
<?php
$content = "<title>";
preg_match_all("/<title>/",$content,$title);
echo $title[0][0];
?>
如何在php中获取<title>...</title>
中的内容。
您是否试图访问DOM,就像在JavaScript中一样?除非您正在阅读现有的HTML页面,或者您正在PHP所生成的页面上使用输出缓冲,否则您无法在PHP中执行此操作。
在这种情况下,你可以使用:
<?php
$content = $the_entire_html_page_loaded_from_somewhere;
preg_match('/<title>(.*)<\/title>/', $content, $title);
print_r($title);
?>
要匹配*什么*当你的内容只包含``
你可以停止使用你的奇怪的标签标题系统吗?这就是标签的用途。 – Bobby 2011-02-10 07:17:08