Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i remove <div> tag from variable without using strip_tags?

Tags:

html

regex

php

Here is my code

$str="<div>this is the variable</div>";

I want to remove its html tag <div> without using strip_tags. I need $str="this is the variable" Because my server is not working with strip_tags. I know it is possible with preg_replace. but i dont know regex. So please suggest me the solution.

Thank you in advance.

like image 930
Krishna Karki Avatar asked Feb 03 '26 22:02

Krishna Karki


1 Answers

This variant works also if the DIV tag contains attributes:

$str = '<div id="some_id">this is the variable</div>';
$str = preg_replace('/\<[\/]{0,1}div[^\>]*\>/i', '', $str);
echo $str;
like image 117
CoursesWeb Avatar answered Feb 06 '26 12:02

CoursesWeb