0

I want to filter url's like bartender/profili/20/friends using preg_match.

Regex:

/^\/bartender\/profili\/d+\/friends/

Unfortunately it doesn't work.

Could somebody help?

6 Answers 6

3

Do you want to capture the \d+? If so, enclose them in ( ), that should fix it. Also, if you're regex uses /, I'd suggest using a different delimiter, less escaping that way:

<?php
preg_match( 
    '~^/bartender/profili/(\d+)/friends~', 
    '/bartender/profili/20/friends', 
    $matches 
);

var_dump( $matches );

Addendum: and - like everyone already mentioned - you indeed forgot the \ in front of the d+, but that was easy to overlook if you have to escape every / ;)

Sign up to request clarification or add additional context in comments.

Comments

2

You have a slash in front of d+ (and have selected a badly readable delimiter for this situation). Try this:

#^\/bartender\/profili\/\d+\/friends#

2 Comments

I think you don't need to escape forward slash if you use # as a delimiter. No?
@Berry Langerak That'd mean I've made about a zilion bugs so far.
2

you forgot to escape \d+

/^\/bartender\/profili\/\d+\/friends/

2 Comments

@genesis; no, he didn't, please read more carefully before a downvote. Upvoting the answer.
@genesis, you are wrong. Look carefully. I have added an extra backslash before d+
2

You are missing a backslash before d+: \d+

Comments

2

"bartender\/profili\/[0-9]+\/friends" also works. Here's an excellent resource for trying regular expressions: http://www.regextester.com/

Comments

-1

You do not have to escape forward slashes, but you need to escape meta-characters (like 'd' for decimal digits)

^/bartender/profili/\d+/friends/

1 Comment

If / is your delimiter you do have to escape the forward slashes. You ignored the delimiters altogether it seems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.