Jump to content

split string into array with 2 delimiters


nestiq

Recommended Posts

Hello, I looking for a piece of code to chop a string into a array with 2 or more delimiters

example:

this is the text and I want to chop [!FIRST!] here and a bit later [!LAST!] here and if its posible (its not required) [!OPTIONAL!] chop it here

 

and store this in a array like

array(
       [0] => "this is the text and I want to chop "
       [1] => "[!FIRST!]"
       [2] => " here and a bit later "
       [3] => "[!LAST!]"
       [4] => " here and if its posible (its not required) "
       [5] => "[!OPTIONAL!]"
       [6] => " chop it here"
)

If someone can help me with it please

 

grtz Nestiq

this is assuming that your string is separated by spaces.

function addChars($str) {

   $ex = explode(" ", $str);
   foreach($ex as $key=>$values) {

      $values = "[!" . $values . "!]";
   }
}

 

this will however add those two chars to all of your array values..since im not 100% sure what exactly it is that you are asking...

 

 

this will however add those two chars to all of your array values..since im not 100% sure what exactly it is that you are asking...

 

I'm trying to find a way to isolate a part of the string in this case its tis part is seperated in the string by [!FIRST!] and [!OPTIONAL!]

 

the part I wish to use is " here and a bit later " the other parts of the string are useless so I'm gona delete that afterwards

nevermind, I got it :P

 

solution:

$arr = preg_split('/\[!(FIRST|LAST|OPTIONAL)!\]/', $text);

 

btw for some reason, I cant find the edit button in the post above :/

glad you figured it out...i was next going to suggest you use a preg function to isolate your values...and yes the edit button only allows you to edit a post for around 10 minutes i believe.

 

Edit: please mark this thread as solved on the lower left of the thread

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.