Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strip_tags() .... replace tags by space rather than deleting them

Tags:

php

Do you know how to replace html tags with a space character using php?

If I display

strip_tags('<h1>Foo</h1>bar'); 

I get as result "foobar" but what I need to keep words separate is "foo bar".

like image 727
Jakob Alexander Eichler Avatar asked Oct 10 '12 17:10

Jakob Alexander Eichler


2 Answers

$string      = '<h1>Foo</h1>bar'; $spaceString = str_replace( '<', ' <',$string ); $doubleSpace = strip_tags( $spaceString ); $singleSpace = str_replace( '  ', ' ', $doubleSpace ); 
like image 131
user40521 Avatar answered Sep 26 '22 10:09

user40521


Try this.

preg_replace('#<[^>]+>#', ' ', '<h1>Foo</h1>bar'); 
like image 29
Rezigned Avatar answered Sep 22 '22 10:09

Rezigned