62

I am working with a regular expression in PHP. I have the following string:

<img 
src="/files/admin/hotel_website.gif" alt="Go To The Hotel's Web 
Site" align="absmiddle" border="0" class="hotel_icon" />

This string contains carriage return and line feed characters.

I want my regular expression to replace html img tags with IMG but this does not work with the above text.

I discovered it contained these characters by looping through each character in the string and printing out the hexadecimal representation which can be found here (http://pastebin.com/ViNdBsRV).

Here is my regular expression:

strip_tags(preg_replace('/^\s*<img\s*.*\/?>\s*$/i', '[IMG]', $test));

Appreciate the help.

1
  • Can you please show me string do you expect from the start string ?
    – KodeFor.Me
    Commented Sep 23, 2011 at 11:45

3 Answers 3

133

To match new lines: [\n\r]+ (matches one or more new line characters)

To match whitespace: \s+ or [\n\r\s]+ (It matches one or more space characters and one or more new line characters. Both regexes are equal.)

9
  • I added this to my regular expression but it does not work; '/^\s*<img[\s\r\n]*.*\/?>\s*$/i'
    – Mr B
    Commented Sep 23, 2011 at 11:10
  • Don't forget that you have to use an interpreted string with quotation marks for this to be effective! ' will just result in the literal.
    – Kzqai
    Commented Nov 10, 2014 at 18:44
  • 1
    @SankalpKotewar I feel like you need the same. I didn't test it, but the \n stands for LF while the \r stands for CR, (At least I hope I am not wrong.). Did you try it with just the [\n\r]?
    – KodeFor.Me
    Commented Jul 1, 2019 at 13:18
  • 1
    \s implicitly matches \r and \n too, so [\n\r\s] is superfluous and just equivalent to \s.
    – Marcel
    Commented Apr 22, 2023 at 15:09
  • 1
    The bad advice is: [\n\r\s] -- that should be reduced to \s because \r and \n are found within \s. This is what Marcel's comment was trying to tell you. Commented Dec 15, 2023 at 6:31
12

This:

preg_replace("#<img.+?/>#is", '[IMG]', $test)

Personally when I'm making a regular expression I always try to go the shortest/simplest. Here you want to replace an entire tag, which starts with '<img' and ends with '/>', '.+?' is a non greedy (lazy) catch. And for the modifiers 'i' for the case and 's' to . the possibility to be a new lines.

More on greedyness vs lazyness : http://www.regular-expressions.info/repeat.html
More on modifiers: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

3
  • This works as expected, thanks for the comprehensive explanation. I tweaked it slightly to fit other cases too: /<img.+?\/?>\s*$/is
    – Mr B
    Commented Sep 23, 2011 at 11:58
  • @Sid You don't need to add \s$ my solution already works to replace <img /> on a whole page (this is how I tested the regex).
    – 3on
    Commented Sep 23, 2011 at 12:34
  • I added $ because I'm getting the html string from a database table.
    – Mr B
    Commented Sep 26, 2011 at 9:08
5

This worked for me it matches multiple spaces and multilines, also any other character or symbol.

[\S+\n\r\s]+

Hope this helps anyone.

It matches for example:

stpd  : asdfasdf
this is also matching ***
1
  • 15
    Puttting a + inside [] makes it match a literal plus. \S is opposite of \s, so you're basically matching any character. [\s\S]+ is the solution as . does not normally match newline characters. There's not much use for this match itself.
    – bradlis7
    Commented Jan 20, 2015 at 17:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.