1

I want to match every string like this

<img src="whatever" whatever alt="whatever" whatever height="any number but not 162" whatever />

in other words i want to match every string that, after the "link" contain whatever except the number 162 (entire number and not only the single character). I use this

function embed($strr) {
    $strr = preg_replace('@<img.*src="([^"]+)"(?:[^1]+|1(?:$|[^6]|6(?:$|[^2]))) />@is', '[img]$1[/img]', $strr);
 return $strr;
}

but this don't match everything that contain 1 and not 162. How can i solve?

Herbert
  • 5,698
  • 2
  • 26
  • 34
korsak
  • 11
  • 1

2 Answers2

5

Instead of Regular Expression you can also use XPath which is specifically designed to extract information from structured markup documents. To get all the img nodes in the document not containing 162 for the height attribute, you would use

//img[not(contains(@height, 162))]

which I personally think is much easier to read than the Regex. Assuming that you just dont want the img nodes with fixed height of 162 instead of all that have 162 in the attribute, e.g. 2162 or 1623, etc, you can just do

//img[@height != 162]

There is various XML/HTML parsers that allow you to use XPath. For a decent list, see

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
2

You can use a negative lookahead like this

height="(?!162)([^"]+)

See it here on Regexr

(?!162) is a negative lookahead, it ensures, that "162" is not following at this position, but it does not match it.

I am not sure what you exactly want to match, but I think you get the idea.

stema
  • 90,351
  • 20
  • 107
  • 135