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
https://forums.phpfreaks.com/topic/121360-php-split-possible-preg_match-needed/
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.

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.

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="/""
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.