1

I'm a total newbie with preg_replace...

Basically I have a long html string and I'd like to remove from it all img tags that have Tracker.php in their SRC

ex : <img src='http://website/Tracker.php?var1=5&var2=8'>

And I have tried $ct=preg_replace("/<img[^>]+\>/i", " ", $string); to remove <img> tag from string and it's not working properly.

Thank you in advance

2
  • 1
    Show us what you have attempted so far. Commented Sep 11, 2015 at 10:36
  • @GiamPy So far i found : $ct=preg_replace("/<img[^>]+\>/i", " ", $string); But this will remove all img tags in the String not just the ones with tracker.php in their src... Thank you Commented Sep 11, 2015 at 10:40

2 Answers 2

2

You could try this:

<?php
$var = "abc<img src='http://website/Tracker.php?var1=5&var2=8'>def";
echo 'Before replace: <textarea style="width:800px;">' . $var . '</textarea><br/>';
$var = preg_replace('~<img[^>]{1,}Tracker.php[^>]{1,}>~', '', $var);
echo 'After replace: <textarea style="width:800px;">' . $var . '</textarea><br/>';
?>

I hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

How to do it in Laravel blade?
0

Sorry to post after a good reply. I was testing my solution in the meanwhile and this works fine too. I add the case insensitive options and short quantifier.

Have a nice day!

$str = preg_replace( '/<img[^>]*Tracker[^>]*>/i', '', $var );

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.