Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 135,919 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,585 people online right now. Registration is fast and FREE... Join Now!




Split

 
Reply to this topicStart new topic

Split

Dr. Tim
23 Sep, 2002 - 03:29 PM
Post #1

D.I.C Addict
****

Joined: 20 Mar, 2002
Posts: 993

I want to split a var at the first "." encountered. Help please.
User is offlineProfile CardPM
+Quote Post

iordy
RE: Split
23 Sep, 2002 - 03:58 PM
Post #2

New D.I.C Head
*

Joined: 5 Mar, 2002
Posts: 45

You could use preg_split or split with a limit of 1 set but it's just as easy to use a match.

CODE

<?

$text = 'this is some 1.5 text. this is some more. and so is this.';

# matches '<dot><space>' so that it should split at the
# end of a sentence, not within a number such as 1.5
if(preg_match('/(.*?\. )(.*)/',$text ,$match)) {
   array_shift($match); # remove first match because it's the whole string.
   print_r($match); # $match[0] is before <dot><space> $match[1] is after.
} else {
   print $text;
}

?>

User is offlineProfile CardPM
+Quote Post

Dr. Tim
RE: Split
24 Sep, 2002 - 03:55 PM
Post #3

D.I.C Addict
****

Joined: 20 Mar, 2002
Posts: 993

i just need an example of the split function
User is offlineProfile CardPM
+Quote Post

iordy
RE: Split
25 Sep, 2002 - 06:01 AM
Post #4

New D.I.C Head
*

Joined: 5 Mar, 2002
Posts: 45

Why with the split function? The code I gave does exactly what you asked for.

You said at the first '.' not every '.' so I would suggest the above method to split at the first '.'. You could use:
CODE

$text_split = split('[.]', $text, 1);


but then it will split when it sees a dot regardless of if it's contained within a number or not, this could lead to unexpected results.

If you want to split at all dots then all you need to do is remove remove the limit. The manual should spell out any other questions regarding the split() or preg_split() functions.

http://www.php.net/manual/en/function.split.php
http://www.php.net/manual/en/function.preg-split.php
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:03AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month