MudGuard: alt="" zu allen <img> tags hinzufügen falls nicht vorhanden.

Beitrag lesen

Hi,

das ist ungenügend. Im Grunde bräuchtest Du lookahead oder lookbehind, um festzustellen, dass am Ende Deines Tags kein alt-Attribut enthalten ist/war, um es dann nachträglich zu ersetzen.

Ich versuche etwas ins Blaue hinein (also ungetestet):

[code lang=php]$gefunden = preg_match_all('~(?is)<img (?:(?!alt=").)+/?>~', $html, $imgs);

Es wäre unfair, nur zu sagen, geht nicht.
Nach etwas Nachdenken bin ich jetzt zu diesem Monster gekommen:

(?is:<img(?:\s+(?!alt)[a-z]+\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|(?:[-._:a-z0-9]*)))*\s*/?>)

Hier noch die etwas übersichtlichere Variante:

(?isx:                      (?# we are searching case insensitive, across lines, and the regex allows whitespaces)
<img                        (?# tag opener)
  (?:                       (?# attribe list start)
     \s+                    (?# there must be whitespace before the attribute name)
     (?!alt)                (?# name of attribute must not be alt - we are searching for img elements without alt attribute)
     [a-z]+                 (?# the attribute name - can't be anything but letters )
     \s*                    (?# optional whitespace )
     =                      (?# the equal character (img does not have any empty attributes, so it must be there))
     \s*                    (?# optional whitespace )
     (?:                    (?# there are several possibilities for attribute values)
        (?:                 (?# 1st variant: in "")
          "
          [^"]*             (?# 1st " is terminating the value)
          "
        )
        |                   (?# OR )
        (?:                 (?# 2nd variant: in '')
           '
           [^']*            (?# 1st ' is terminating the value)
           '
        )
        |                   (?# OR (3rd variant for HTML only, remove it for XHTML only))
        (?:
           [-._:a-z0-9]*    (?# 3rd variant: not enclosed, allows limited range of chars)
        )
     )                      (?# end of attribute value
  )*                        (?# attribute list end - attribute number is 0 or more)
  \s*                       (?# optional whitespace)
  /?                        (?# optional /, remove for HTML only, remove ? for XHTML only)
  >                         (?# tag closer)
)

Ich konnte bisher keinen validen Fall konstruieren, bei dem der Regex einen false positive oder false negative ergeben hätte.
Was aber nicht heißt, daß es diesen Fall nicht gibt.
Wer einen findet, möge ihn bitte nennen.

cu,
Andreas

--
Warum nennt sich Andreas hier MudGuard?
O o ostern ...
Fachfragen unaufgefordert per E-Mail halte ich für unverschämt und werde entsprechende E-Mails nicht beantworten. Für Fachfragen ist das Forum da.