RE: [PHP] converting string into array with regex

From: Date: Fri, 05 Dec 2003 12:00:52 +0000
Subject: RE: [PHP] converting string into array with regex
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-171969@lists.php.net to get a copy of this message
On vrijdag 5 december 2003 12:23 Burhan Khalid told the butterflies: > Adam i Agnieszka Gasiorowski FNORD wrote: > > > How would you specify a regex to > > convert string into array using preg_split? > > Is there some symbol specyfying a place between letters ? > > > > s t r i n g => array('s', 't', 'r', 'i', > > 'n', 'g') > > ^ ^ ^ ^ ^ > > You can access a string's characters with an index without declaring > it as an array. > > $string = "foo"; > echo $string{0}; //outputs f > > http://www.php.net/substr (has an example) > > Maybe this will solve your problem without using preg_split? > > -- > Burhan Khalid > phplist[at]meidomus[dot]com > http://www.meidomus.com > ----------------------- > "Documentation is like sex: when it is good, > it is very, very good; and when it is bad, > it is better than nothing." You kinda need to realize that $String = 'FooBar'; print $String[4]; also prints out an 'B'. But this is adviced AGAINST because it is the same syntax as you do for array elements. To get a real array, do something like this: $Array = Array(); for($i=0;$i<strlen($String);$i++) $Array[] = $String{$i}; But if you have a string with char-space-char stuff, split(' ', $String); would do too. Wouter

« previous php.general (#171969) next »