1

I want to remove all <td></td> from source String in PHP. But don't know how to do it. I tried but it does not work.

preg_replace('/(?:<|&lt;)td(?:>|&gt;)(?:<|&lt;)\/td(?:>|&gt;)/', '', $sourceString);

Please tell me how to do it in PHP

1
  • 1. <td></td> 2. <td>blahblah</td> so what u want to recieve? 1. empty string 2. blahblah or empty string? Commented Sep 5, 2015 at 11:33

2 Answers 2

3

PHP example

<?php
$out =  preg_replace('/(?:<|&lt;)td(?:>|&gt;)(?:<|&lt;)\/td(?:>|&gt;)/', '', $sourceString);
?>

if your regex is correct in $out is your result :)

but there is one more simple way strip tags

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@MinhLe please can you confirm my answer :) it realy helps.
1

your pattern is looking for

<td></td>

but it won't find

<td>test</td>

There is a litte "|" missing. That one fits:

<?php
$out =  preg_replace('/(?:<|&lt;)td(?:>|&gt;)|(?:<|&lt;)\/td(?:>|&gt;)/', '', $sourceString);
?>

you can try it here https://de.functions-online.com/preg_replace.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.