Lc3 Posted December 12, 2008 Share Posted December 12, 2008 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? ??? Quote Link to comment https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/ Share on other sites More sharing options...
Mchl Posted December 12, 2008 Share Posted December 12, 2008 strip_tags Quote Link to comment https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-713522 Share on other sites More sharing options...
Lc3 Posted December 12, 2008 Author Share Posted December 12, 2008 Okay so it was a case of KISS then? stip_tags() works, thanks. P.S. For the record, can anyone tell me why my preg_replace() attempt was broken? So I can learn for next time that I actually *DO* need to use regex. Quote Link to comment https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-713525 Share on other sites More sharing options...
.josh Posted December 12, 2008 Share Posted December 12, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-713958 Share on other sites More sharing options...
nrg_alpha Posted December 14, 2008 Share Posted December 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-715254 Share on other sites More sharing options...
RussellReal Posted December 18, 2008 Share Posted December 18, 2008 /(<[^>]+?>|^[^<]+?>|<[^>]+?$)/ Quote Link to comment https://forums.phpfreaks.com/topic/136649-remove-all-tags-help/#findComment-718596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.