Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


explode generating an empty element in an array

Posted on 2007-08-07
Medium Priority
Last Modified: 2013-11-05
I'm using explode to delimit a string, for example:

//$testString = apple+orange";
$testStringArr = explode("+", $keywords);

The output of the size of the $testStringArr  both are 2.

Any suggestion is highly appreciated,

Question by:lilyyan
LVL 24

Accepted Solution

glcummins earned 400 total points
ID: 19649153
This is expected behavior for explode(). If you wish to avoid it, you can use trim() to remove any leading or trailing delimiters:

$testStringArr = explode("+", trim($keywords, '+'));

Author Comment

ID: 19649580
well, if i use $testString="apple+"; the twp elemens are "apple" and "". (one empty string)

Assisted Solution

Sisson earned 200 total points
ID: 19649659
Still, as glcummins said, that is the expected behaviour.  If you want to only split when there is a word following the character, use preg_split

preg_split("/\\+/", $testString, -1, PREG_NO_EMPTY);

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question