Jump to content

Remove all tags - help


Lc3

Recommended Posts

I'm trying to remove tags in a given string for example:

 

Testing... <b>Hello</b> <img src="something.jpg" /> World!

should become

Testing... Hello  World!

 

And I'm trying this code:

 

preg_replace('<.*?>', '', $subject)

 

but it always returns the empty string. What did I do wrong?  ???

Link to comment
https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/
Share on other sites

You don't have any delimiters in your regex (the thing that tells pcre where the beginning and end of the pattern is, like /../).  You should have gotten an error message from that, so maybe you have error reporting turned off.  Other than that, it should have worked.  Another way to write it is like so:

 

echo preg_replace("/<[^>]*>/","",$string);

 

Link to comment
https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-713958
Share on other sites

You don't have any delimiters in your regex (the thing that tells pcre where the beginning and end of the pattern is, like /../).  You should have gotten an error message from that, so maybe you have error reporting turned off.

 

Nope.. that pattern is correct syntactically. Delimiters pairs can be < and >.. but of course, the end result is that only everything inside those are considered the pattern. In fact, it is perfectly acceptable to use opening punctuation such as {, (, < and [.. but then the opposite (closing) corrosponding punctuation marks must be used to successfully close delimitation..

 

To the OP, This is why it is not advisable to use such characters as delimiters, and instead stick to stuff like /, #, ! or ~ as some 'safer' examples... otherwise you fall into one of those regex 'gotchas' that leave you scatching your head and wondering why your regex solution isn't working.

Link to comment
https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-715254
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.