Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.