Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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 如何在使用strip_tags()后从字符串中删除纯文本_Php_Regex_Strip Tags - Fatal编程技术网

Php 如何在使用strip_tags()后从字符串中删除纯文本

Php 如何在使用strip_tags()后从字符串中删除纯文本,php,regex,strip-tags,Php,Regex,Strip Tags,因此,我有一个字符串,我使用strip\u tags()函数删除除IMG之外的所有标记,但IMG元素旁边仍然有纯文本。这里是一个直观的例子 $myvariable = "This text needs to be removed<a href='blah_blah_blah'>Blah</a><img src='blah.jpg'>" 这就是变量实际持有的东西 提前感谢请使用带标签显示您的全部代码 您可以尝试:preg_replace(“~.*(]+>)~”

因此,我有一个字符串,我使用
strip\u tags()
函数删除除IMG之外的所有标记,但IMG元素旁边仍然有纯文本。这里是一个直观的例子

$myvariable = "This text needs to be removed<a href='blah_blah_blah'>Blah</a><img src='blah.jpg'>"
这就是变量实际持有的东西


提前感谢

请使用
带标签
显示您的全部代码


您可以尝试:
preg_replace(“~.*(]+>)~”、“$1”、$myvariable)

您可以很好地提取所需的值,而不是替换某些内容:

$description = 'crazy stuff<a href="https://websta.me/p/1337373806024694030_327078936"><img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2"></a>';

正则表达式。。但是需要更多地了解可能的Strings去掉我刚才添加的变量。提前感谢检查我刚才添加的变量。提前感谢如果您使用上面的preg_replace,您将只保留img标记,这不是您想要的吗?如果我只需要img标记部分呢?那么您可以使用解析器或
(<(\w+).+</\2>)
<?php
$regex = '~(<(\w+).+</\2>)~';
$string = 'crazy stuff<a href="https://websta.me/p/1337373806024694030_327078936"><img src="https://scontent.cdninstagram.com/t51.2885-15/e15/14287934_1389514537744146_673363238_n.jpg?ig_cache_key=MTMzNzM3MzgwNjAyNDY5NDAzMA%3D%3D.2"></a>here as well';

if (preg_match($regex, $string, $match)) {
    echo $match[1];
}
?>