Re: Split string
| From: | Jim Lucas | Date: | Wed, 02 May 2007 21:30:17 +0000 |
| Subject: | Re: Split string | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-254283@lists.php.net to get a copy of this message | ||
Stut wrote:
Richard Lynch wrote:<?php $myString = "Here is my first, attempt!"; $answer = preg_split('|[, ]|', $myString, 2); var_dump($answer); ?> -- Enjoy, Jim Lucas Different eyes see different things. Different hearts beat on different strings. But there are times for you and me when all such things agree. - RushOn Wed, May 2, 2007 4:55 am, Lester Caine wrote:Will give more than 2 parts for strings containing both or multiples. Now please irradiate your hands. -Stut This should work for you.Fredrik Thunberg wrote:Hnmmm. Okay, I'll take a shot at it: $answer = preg_split('|[, ]|', $myString); var_dump($answer);Lester Caine skrev:I'm glad I asked now, that is a lot nicer than trying to get the preg_split thing working :)Can someone with a few more working grey cells prompt me with the correct command to split a string. The entered data is names, but I need to split the text up to the first space or comma into one string, and the rest of the string into a second. It's the 'first either space or comma' that eludes me at the moment :( In know it's probably obvious, but it always is when you know the answer.$myString = "John Doe"; $pos = strpos($myString, " ") < strpos($myString, ",") ? strpos($myString, " ") : strpos($myString, ","); $part1 = substr($myString, 0, $pos); $part2 = substr($myString, $pos);