![]() |
[PHP] Regular Expressions: Een sting juist NIET willen hebben....
Hoi mensen, ik heb de volgende code geschreven om de url in een meta redirect tag te kunnen wijzigen:
Code:
// URL met http:// converteren Als je nog anderen op of aanmerkingen hebt aan mijn regex, zijn die ook welkom ;). Het is de eerste keer dat ik ermee werk :). |
1. ik zou met Perl RegExps werken
2. zoekwerk op php.net: http://nl3.php.net/manual/en/ref.regex.php comment van regex at dan42 dot cjb dot net: It's easy to exclude characters but excluding words with a regular expression is a bit more tricky. For parentheses there is no equivalent to the ^ for brackets. The only way I've found to exclude a string is to proceed by inverse logic: accept all the words that do NOT correspond to the string. So if you want to accept all strings except those _begining_ with "abc", you'd have to accept any string that matches one of the following: ^(ab[^c]) ^(a[^b]c) ^(a[^b][^c]) ^([^a]bc) ^([^a]b[^c]) ^([^a][^b]c) ^([^a][^b][^c]) which, put together, gives the regex ^(ab[^c]|a[^b]c|a[^b][^c]|[^a]bc|[^a]b[^c]|[^a][^b]c|[^a][^b][^c]) Note that this won't work to detect the word "abc" anywhere in a string. You need to have some way of anchoring the inverse word match like: ^(a[^b]|[^a]b|[^a][^b]) ;"ab" not at begining of line or: (a[^b]|[^a]b|[^a][^b])& ;"ab" not at end of line or: 123(a[^b]|[^a]b|[^a][^b]) ;"ab" not after "123" I don't know why "(abc){0,0}" is an invalid synthax. It would've made all this much simpler. |
Mja dat is dus de omweg :p....
Perl ben ik ook mee bezig, maar ik vind PHP toch net iets handiger :). btw: Als ik "url=(?!http://)" probeer(wat dus ook zou moeten werken...), geeft ie weer opeens een compile error :-/. Citaat:
Citaat:
|
Citaat:
|
Oww op die manier :D Ja klopt.
|
Weet iemand wat er mis is met deze regel?
Code:
$line = preg_replace("/url=/(\"|'|)/http:\/\/.*/(\"|'| |)", " ", $line); <meta http-equiv="Refresh" content="0; surf.php?URL=http://willekeurigeurldievervangenmoetworden"> |
Citaat:
wat is de fout trouwens? |
Nu heb ik:
$line = preg_replace("\ url=http:\/\/(\"|'| ||)(.*)(>|\"|')\i","url=/proxy/surf.php?url=http://$1hond$2", $line); En krijg de melding: Delimiter must not be alphanumeric or backslash |
$line = preg_replace("/ url=http:\/\/(\"|'| ||)(.*)(>|\"|')/i","url=/proxy/surf.php?url=http://$1hond$2", $line);
Hierbij werkt het wel, maar het doet absoluut niet wat ik wil.... Ik wil dat hij zoekt naar een: "url=http://", daarachter mag " of '. Vervolgens komt er een url (deze moet dus vervangen worden), en daarachter moet een ", ' of > staan. |
Citaat:
|
Ok... dat wordt weer zoeken -_-'
|
*sigh* ik kom echt niet verder :(
|
Citaat:
|
Ik denk dat laatste maar :o.
|
Alle tijden zijn GMT +1. Het is nu 00:34. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.