Keep multi-character delimiter

I have a string

$string = "hi!n!my!n!name!n!is!n!bob";

I want to split it by the '!n!' I've tried something like:

$splitstring = preg_split("!n!", $string,-1, PREG_SPLIT_DELIM_CAPTURE);

but it removes all instances of 'n' instead of !n! so if i print_r the array, it'll look something like:

Array( [0]=>hi! [1]=>!my! [2]=>! [3]=>!ame! [4]=>!is! [5]=>!bob )

it would split every occurrence of 'n'. not !n! (a 3 character delimiter) like I wanted.

Does anyone know how to solve this? Thanks in advance

-------------Problems Reply------------

You need delimiters (e.g. /) in your regular expression:

$splitstring = preg_split("/!n!/", $string,-1, PREG_SPLIT_DELIM_CAPTURE);

Okay, first of all thanks for all your replies. using / worked but it removed all of the delimiters (!n!). I suspected that PREG_SPLIT_DELIM_CAPTURE wasn't working. So, I found this. The fix was something like:

$splitstring = preg_split("/(!n!)/", $string,-1, PREG_SPLIT_DELIM_CAPTURE);

putting the brackets around the delimiter outputted:


Hope this helps

You could just use explode:

$parts = explode('!n!', $string);

To reproduce the string you started with you can do this:

$string = implode('!n!', $parts);

Finally, if you're looking to keep the delimeter(s) you can always add them back in. It's not clear whether you want them on the start or end of each entity (as your starting string uses them purely as a separator). You could do that like so:

foreach ($parts as &$part)
$part = '!n!'.$part.'!n!';

Category:php Views:9 Time:2019-03-06
Tags: php

Related post

  • How to split a string with multi-character delimiter in vb - 2010-06-21

    How should I split a string separated by a multi-character delimiter in VB? i.e. If my string is say - Elephant##Monkey, How do I split it with "##" ? Thanks! --------------Solutions------------- Dim words As String() = myStr.Split(new String() { "##

  • How to make multi character delimiter in Coldfusion for cfloop? 2010-01-27

    I have a String variable which has dynamic user entered text EX:- <cfset setPars="SPTO_DATE('04/11/2009 11:59:59 PM', 'MM/DD/YYYY HH:MI:SS AM')SP(L','MN)>' Now If I use SP as the delimiter in CFloop as below <cfloop index="i" from="1" To="#L

  • Java CSV parser with string separator (multi-character) 2011-12-28

    Is there any Java open source library that supports multi-character (i.e., String with length > 1) separators (delimiters) for CSV? By definition, CSV = Comma-Separated Values data with a single character (',') as the delimiter. However, many othe

  • multi-character parentheses in syntax table 2009-09-05

    I know that I can have multi-character comment delimiters, but how about multi-character parentheses, e.g. if I wanted to treat the character sequence "%{" as an opening parenthesis and "}%" as a closing one like this %{ }% --------------Solutions---

  • how to keep multi session live together 2010-02-03

    Thanks a lot and sorry for bothering.. I'm still a PHP rookie, I am having a few problems trying to understanding/finding out how to keep multi session live together works.. here my existing sessions e.g. session_start(); // If no session exists, cre

  • Is \n multi-character in C? 2010-09-08

    I read that \n consists of CR & LF. Each has their own ASCII codes. So is the \n in C represented by a single character or is it multi-character? Edit: Kindly specify your answer, rather than simply saying "yes, it is" or "no, it isn't" ---------

  • Multi-character character sequence in 3rd party library 2011-07-29

    I'm using a 3rd party library for an iOS project I work on, and I'm down to one warning left in the project, namely on this line of code [NSNumber numberWithUnsignedLongLong:'oaut'] And the warning is Multi-character character constant I suck at C, s

  • Python readline module: Set 2 character delimiter 2011-09-02

    I'm using the readline module to autocomplete names (firstname lastname). I want to set a 2 character delimiter, but by setting readline.set_completer_delims(', ') it accepts both the comma and the whitespace as a delimiter. But I only want the combi

  • Multi-character constant warnings 2011-10-13

    This question already has an answer here: What do single quotes do in C++ when used on multiple characters? 4 answers Why is this a warning? I think there are many cases when is more clear to use multi-char int constants instead of "no meaning" numbe

  • android keyboard multi-character key 2011-10-16

    How do I specify that I want a key press to do more that 1 character? Like () or a smiley? More specifically, for my problem, how do I do this using the popupKeyboard? I want the user to be able to choose a selection and have that selections image an

  • Shut up multi-character constant warning 2011-11-03

    For very specific reasons, I have an enum of this kind: typedef enum RSGameType { RSGameTypeUnknown = 0, RSGameType1v1 = '1v1', RSGameType2v2 = '2v2', RSGameType3v3 = '3v3', RSGameType4v4 = '4v4', RSGameTypeCustom = 'Cust' } RSGameType; However, Clan

  • Python split string with multiple-character delimiter 2011-11-10

    say I have the following string: "Hello there. My name is Fred. I am 25.5 years old." I want to split this into sentences, so that I have the following list: ["Hello there", "My name is Fred", "I am 25.5 years old"] As you can see, I want to split th

  • How to resolve warning: multi-character character constant in Linux C++ 2011-11-23

    #ifdef WIN32 # define TARGET_OS 'W_NT' I am getting C++ warning in Linux multi-character character constant. at the below line. How to resolve this warning. #if TARGET_OS == 'W_CE' --------------Solutions------------- String constants use double quot

  • how to resolve c++ warning: multi-character character constant 2011-11-23

    #ifdef WIN32 # define TARGET_OS 'W_NT' I am getting C++ warning in Linux multi-character character constant. at the below line. How to resolve this warning. #if TARGET_OS == 'W_CE' --------------Solutions------------- String constants use double quot

  • what do i need to keep multi displays i want my PC on my TV frustrating ! 2014-03-24

    what do i need to keep multi displays i want my PC on my TV frustrating ! --------------Solutions------------- Connect your computer to a TV: Dual monitor setup: http://www.mic

  • iText tables - how to keep multi row cells on one page? 2013-05-13

    I'm creating a table in iText that lists events for a date. If there are mutliple events for a date, I set the rowspan property of the date-cell to the number of events. I'm currently using a row for each event, since I want to display additional inf

  • Regexes and multiple multi-character delimeters 2008-09-18

    Suppose you have the following string: white sand, tall waves, warm sun It's easy to write a regular expression that will match the delimiters, which the Java String.split() method can use to give you an array containing the tokens "white sand", "tal

  • Java - keeping multi-version application from splitting codebase 2009-05-20

    I am writing an application that will ship in several different versions (initially around 10 variations of the code base will exist, and will need to be maintained). Of course, 98% or so of the code will be the same amongst the different systems, an

  • Java string.split - by multiple character delimiter 2011-09-20

    I would like to parse entire file based on all the possible delimiters like commas, colon, semi colons, periods, spaces, hiphens etcs. Suppose I have a hypothetical string line "Hi,X How-how are:any you?" I should get output array with items Hi,X,How

  • Hijacking UnhandledException to keep Multi-Threaded Application Running 2012-01-19

    Situation Well, as you can see below, I have a main App, creating a thread, which creates a bunch of background workers. When the RunWorkerCompleted is fired on some BG worker, I sometimes end up dropping an Unhandled Exception, (for a reason I have

  • Split on multiple characters while keeping the character in the result 2012-02-25

    I want to split on '0' and '1' while keeping those characters in the split result. How can I do this in C#? e.g. "012345" => 0,1,2345 I tried Regex.Split(number, @"(?=[01])"); but I get "", 0, 12345 as the result The following seems to work, excep

  • Extract a single character from a multi-character cell 2014-05-17

    EXCEL 2007: I would like to take the contents of a single cell with 17 alpha-numeric characters and place each character in a single cell, thus creating 17 new cells with one alpha-numeric character each. Can you help? --------------Solutions--------

  • Javascript: quick script to line up pipe-delimited text (or any-character-delimited text) 2009-10-30

    Background: I've written this before but I don't like the approach. The reason is because Javascript does not have "sprintf" and that is something I use heavily if the language supports it. Question: How would you use javascript to go from BEFORE to

  • How to make a multi-character parameter in UNIX using getopt? 2010-10-20

    I'm trying to make a getopt command such that when I pass the "-ab" parameter to a script, that script will treat -ab as a single parameter. #!/bin/sh args=`getopt "ab":fc:d $*` set -- $args for i in $args do case "$i" in -ab) shift;echo "You typed a

  • Regex for multi character strings embedded in rest of match? 2011-05-13

    How can I match something like "AB|CD|EF" followed by "12|34" and get, for instance, "AB12" back? If the string was "zzAB34zz" I'd get "AB34", "zzABCD12" and get "CD12", etc? --------------Solutions------------- No need for named groups here, really:

Copyright (C), All Rights Reserved.

processed in 0.120 (s). 11 q(s)