Jump to content

pattern help


Go to solution Solved by requinix,

Recommended Posts

hey guys, i'm trying to get back in to the swing of things after a lot of time out from programming, and i'm struggling with this simple regex pattern if i could get some help please.

 

i've decided to go over some of my code and try and re-write things better....starting with my framework.

 

 

here is my pattern:

/^\/?(i|s)?:+([A-Za-z0-9_-])$/

what im trying to do is match:

 

news/i:news_id   - returning strings i (if there) and news_id

 

as the string is uri it could contain a forward slash at the beginning.

if (preg_match_all('/\/?(i|s)?:+([A-Za-z0-9_-])/', 'my-page/i:foo/:bar', $fixed_parameters))
{
    print_r($fixed_parameters);  
}

result:

Array
(
    [0] => Array
        (
            [0] => i:f
            [1] => /:b
        )

    [1] => Array
        (
            [0] => i
            [1] => 
        )

    [2] => Array
        (
            [0] => f
            [1] => b
        )

)

idealy what i'd like is a result like this:

Array
(
    [0] => Array
        (
            [0] => i
            [1] => foo
        )

    [1] => Array
        (
            [0] => 
            [1] => bar
        )

)

thanks guys

Link to post
Share on other sites
  • Solution

You aren't repeating that character set at the end.

 

As for the format, [0] will always be the full match. Best you can get is "i" as [1] and the identifier as [2] by using what you have now with the PREG_SET_ORDER flag.

  • Like 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.