Jump to content

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

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.