/    Sign up×
Community /Pin to ProfileBookmark

regexp not included

[font=technical][size=3]in a regular expression how do you state that you do not want a set of characters to appear?[/size][/font]

to post a comment
PHP

25 Comments(s)

Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]Negate the character set.[/font]

<i>
</i> # Will find "Find this "
# but not "Find this not this"

preg_match("#Find this [^not this]#i", $haystack, $matches);
Copy linkTweet thisAlerts:
@artemisauthorSep 11.2004 — [font=technical][size=3]thank you[/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]Happy to help; keep the PHP reference manual by your side. It has complete documentation for everything, including Regular Expressions. I always use the Perl compatible RegEx functions, myself, but there are also [url=http://www.php.net/ereg]ereg[/url] functions for PHP only. I don't know exactly all the differences, and though I doubt negation is one of them, it is a possibility. So if you decide to use ereg instead of preg, make sure you double-check with the documentation.[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 11.2004 — [font=technical][size=3]hi,

thanks i have another problem though. i am not using preg instead i have this line in a .htacces file. its to create cruft-free-urls:

[font=monospace][size=5]
[code=php]
RewriteRule ^archives/(.*)/?[^.php]$ /archives/$1.php
[/code]
[/size][/font]

now if a file is called like this:

http://localhost/archives/page/

it works fine, but if it is called like this:

http://localhost/archives/page

for some reason it cuts off the last character, so tries to open the page http://localhost/archives/pag.php, which creates a 404 error.

how do I fix this, is my regexp correct?

cheers[/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]What's the negation for, though? Is there something wrong with the following?[/font]

<i>
</i>RewriteRule ^archives/(.*)/?$ /archives/$1.php [L]
Copy linkTweet thisAlerts:
@artemisauthorSep 11.2004 — [font=technical][size=3]that creates a server error![/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 11.2004 — [font=trebuchet ms]Remove the [L].[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 11.2004 — [font=technical][size=3]it still creates the error because it now loops continuously because it constantly replaces all the characters after the archives/[/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]...It shouldn't. ? It works for me, just fine.

[/font][font=courier new]http://localhost/blog/archives/ed[/font][font=trebuchet ms]

Looks for the file:

[/font][font=courier new]http://localhost/blog/archives/ed.php[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]lol damn, for me it would try to find the file



http://localhost/blog/archives/e.php



hmm, interesting ?[/size]
[/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]I thought you said it did an infinite loop. ? [/font]

<i>
</i> # This is what I'm using...
RewriteRule ^archives/(.*)/?$ /archives/$1.php [L]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]yes it loops with that code but with the original code I posted it removes the last letter

???[/size]
[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]ooo now its just creating a server error??? with this code:



RewriteRule ^archives/(.*)/?$ /archives/$1.php [L]



hmmm[/size]
[/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]Actually your original code works absolutely fine for me as well. It must have something to do with your configurations...[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]also with this code:



RewriteRule ^archives/(.*)/?$ /archives/$1.php [L]



it creates a server error even if I include a trailing slash!

so it doesnt work at all![/size]
[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]do you thinks theres a problem with the settings ?

how would I find out?[/size]
[/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]I've never had this problem before, so I can't really say. I tested again and realized that your original code actually isn't finding anything at all. This works, though, at least for me.[/font]

<i>
</i>RewriteRule ^archives/(.*)/?$ /archives/$1.php [L]


[font=trebuchet ms]I'm going to look in the httpd.conf file to see if I can find anything about it, though there probably isn't anything as mod_rewrite is its own module and not part of the default configuration...[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]what do you mean by [b]isn't finding anything at all[/b]?[/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]It turned out your original code wasn't affecting anything at all, for me.[/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]I can't seem to find anything in [/font][font=courier new]httpd.conf[/font][font=trebuchet ms] that would affect mod_rewrite other than the allowance of the module itself.[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]i did realise one mistake that I did not escape the '.' in '.php' but that hasnt made any difference![/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]Nope, it didn't make a difference at all. My code should work fine, it doesn't loop indefinitely for me.[/font]

<i>
</i>RewriteRule ^archives/(.*)/?$ /archives/$1.php [L]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]yay I fixed it!

i'll just make the code look nice then show the solution.[/size]
[/font]
Copy linkTweet thisAlerts:
@artemisauthorSep 12.2004 — [font=technical][size=3]ok so this line works fine when accessing a page such as

http://localhost/archives/file-one/

[font=monospace][size=5]
[code=php]
RewriteRule ^archives/(.*)/$ /archives/$1.php [L]
[/code]
[/size][/font]

but it does not work when the trailing slash is removed, like so:

http://localhost/archives/file-one/

(note that changing it to (.*)/? did not work for me as it generated a server error.

i found this http://www.engelschall.com/pw/apache/rewriteguide/#ToC7 which made me realise that all I have to do is check that all incoming urls have a trailing slash. so I added this line:

[font=monospace][size=5][code=php]
RewriteRule ^(.+[^/])$ /$1/ [R]
[/code]
[/size][/font]

the only problem with this was that when it tried to access pages with a .php it added a trailing slash so I then changed the line to this to account for that:

[font=monospace][size=5][code=php]
RewriteRule ^(.+[^/|\.php])$ /$1/ [R]
[/code]
[/size][/font]

et voila! it works!

thanks jona for your input I still have no idea why the other code worked for you and not me but that's life![/size][/font]
Copy linkTweet thisAlerts:
@JonaSep 12.2004 — [font=trebuchet ms]Happy to help; glad you found a solution.[/font]
×

Success!

Help @artemis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...