我有以下代码:
<?php echo strip_tags($firstArticle->introtext); ?>
Run Code Online (Sandbox Code Playgroud)
其中$ firstArticle是一个stdClass对象:
object(stdClass)[422]
public 'link' => string '/maps101/index.php?option=com_content&view=article&id=57:greenlands-newest-iceberg&catid=11:geography-in-the-news' (length=125)
public 'text' => string 'GREENLAND'S NEWEST ICEBERG' (length=26)
public 'introtext' => string '<p>A giant chunk of ice calved off the Petermann Glacier on
the northwest side of Greenland this summer. At nearly 100 square miles (260
sq. km) in size, four times the size of Manhattan, th' (length=206)
public 'date' =>
object(JDate)[423]
public '_date' => int 1284130800
public '_offset' => int 0
public '_errors' =>
array
empty
Run Code Online (Sandbox Code Playgroud)
你可以看到$ firstArticle-> introtext引用了字符串:
" <p>今年夏天,格陵兰岛西北侧的彼得曼冰川出现了一大块冰.大约100平方英里(260平方公里),是曼哈顿面积的四倍,"
该<p>标签在此应用中对我的问题,但是用strip_tags绝对拒绝删除它,我想不通为什么.我实际上放弃了strip_tags并试图用regex /<(.|\n)*?> /来做一个preg_replace:
preg_replace('/<(.|\n)*?>/', '', $firstArticle->introtext);
Run Code Online (Sandbox Code Playgroud)
但这也不起作用!当我输出时,如何从该字符串中删除所有HTML标记(匹配与否)?
The*_*can 61
尝试:
<?php echo strip_tags(html_entity_decode($firstArticle->introtext)); ?>
Run Code Online (Sandbox Code Playgroud)
非常奇怪的是条形标签不起作用....
也许你的"<p>"是htmlentity编码的吗?喜欢"< p>" (看看页面的源代码)
otehrwise这将取代所有标签,也取代htmlentity编码的标签,但几乎显而易见的是,这个p标签只是简单的编码,所以首先尝试...
preg_replace('/(?:<|<).*?(?:>|>)/', '', $firstArticle->introtext);
Run Code Online (Sandbox Code Playgroud)