So - bei meinem geschätzten 400. Google-Versuch heute hab ich endlich was gefunden. Auf skdevelopment.com steht Folgendes:
How to denote a backslash in a regular expression.
It is a very common case when you have to replace backslashes "" in some string with common slashes "/".
Of course if this is all which has to be done, using a regular expression would be an overkill. You could do it with the function str_replace().
Still it could be useful to know how to replace backslashes "" in strings with common slashes "/" with a regular expression.
The following code:
<?php
$string = "c:\somepath\somefile.php";
$string = preg_replace("/\/","/",$string);
echo "$string";
?>
would produce a parser error.
To make the replacement of "" with "/" correctly, we would have to use the following code:
<?php
$string = "c:\somepath\somefile.php";
$string = preg_replace("/\\\\/","/",$string);
echo "$string";
?>
It would produce:
c:/somepath/somefile.php
Stimmt. Genau so funktioniert es. Um das jetzt auf mein Beispiel umzulegen:
if(preg_match("/[1-9\$\\\\p*]/",$_POST['wort']))
{
echo"<p><em>Verboten!</p>\n";
}
else
{
echo"<p>Die Eingebe ist OK!</p>\n";
}
Das bewirkt, dass ein $_POST['wort'], in dem eine Ziffer, ein kleines p, ein Dollarzeichen oder ein Backslash sind, als verboten markiert wird, alles andere wird durchgelassen.
Damit ist zwar mein Problem gelöst, aber verstehen tue ich das nicht. Wieso funktioniert \\ und \ (was logisch wäre) funktioniert nicht?
Gustav Gans