Jump to content

PHP split - possible preg_match needed.


dis

Recommended Posts

Hello, I currently have:

 

<?php
  router::register('about/:id="/^\d/"/:title');
?>

 

I need to split the string by '/', unless '/' is found inside the ="" section.

So, I need a function to split the above into an array which should match (hence why explode will not work):

 

Array(
 [0] = about
 [1] = :id="/^\d/";
 [2] = :title
)

 

I can do this via array loops and strpos functions, but that seems slow and stupid.

 

I am wondering if this can be done via a preg_match, returning matches. Any help would be fantastic.

 

Cheers,

- dis.

Link to comment
Share on other sites

Uhh what does this mean:

 

I need to split the string by '/', unless '/' is found inside the ="" section.

 

and what do you want to get out of this:

 

[1] = :id="/^\d/";

 

the string inside the double quotes?

 

This kind of thing can be done really easily with preg_match I just need to know what you want to do.

Link to comment
Share on other sites

Sorry, it is sort of confusing.

 

All I need to get is an array. I am not worrying about anything inside the "" at the moment, just the process of splitting up the original string.

 

Basically I need to split the string by /. However, if / is located inside the quotes ="", I don't want it to split.

 

Some further examples:

 

<?php
router::register('about/:title');
Array(
  [0] = about
  [1] = :title
)

router::register('about/:title="/"');
Array(
  [0] = about
  [1] = :title="/"
)

router::register('about/:title/:id="/^\d/");
Array(
  [0] = about
  [1] = :title
  [2] = :id="/^\d/"
)
?>

 

Those above are the results I am looking for. If you need further explaination, please let me know.

Link to comment
Share on other sites

Uhh

 

preg_match_all ('/(:?.+?(?:=\".+?\")?)(?:\/|$)/s', $str, $matches);

 

$str = 'about/:title="/"';

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "about/"
    [1]=>
    string(10) ":title="/""
  }
  [1]=>
  array(2) {
    [0]=>
    string(5) "about"
    [1]=>
    string(10) ":title="/""
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.