This is a discussion on converting string into array with regex within the PHP General forums, part of the PHP Programming Forums category; How would you specify a regex to convert string into array using preg_split? Is there some symbol specyfying a place ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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') ^ ^ ^ ^ ^ , like this... -- Seks, seksić, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa ) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne kształty... http://www.opera.com 007 |
|
|||
|
On Friday 05 December 2003 19:08, 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') > ^ ^ ^ ^ ^ RTFM! -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * ------------------------------------------ Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general ------------------------------------------ /* A journey of a thousand miles must begin with a single step. -- Lao Tsu */ |
|
|||
|
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." |
|
|||
|
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 |
|
|||
|
Wouter van Vliet wrote:
> 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. No, no spaces between letters (otherways it would be very easy, indeed). So there is no way to match the "space between alphanumeric chars" and split on it? I was trying to avoid the loop solution. -- Seks, seksić, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa ) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne kształty... http://www.opera.com 007 |
|
|||
|
From: "Adam i Agnieszka Gasiorowski FNORD" <agquarx@venus.ci.uw.edu.pl>
> No, no spaces between letters (otherways > it would be very easy, indeed). So there is > no way to match the "space between alphanumeric > chars" and split on it? I was trying to avoid > the loop solution. Of course there is. In fact, this example, direct from the manual, does exactly what you want... <?php $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars); ?> Imagine that!---John Holmes... |
|
|||
|
"CPT John W. Holmes" wrote:
> From: "Adam i Agnieszka Gasiorowski FNORD" <agquarx@venus.ci.uw.edu.pl> > > > No, no spaces between letters (otherways > > it would be very easy, indeed). So there is > > no way to match the "space between alphanumeric > > chars" and split on it? I was trying to avoid > > the loop solution. > > Of course there is. In fact, this example, direct from the manual, does > exactly what you want... > > <?php > $str = 'string'; > $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); > print_r($chars); > ?> > Imagine that!---John Holmes... LOL! :8]] You are SO right. I just KNEW the solution existed, I must have even read it a long time ago in the very manual... Sorry for wasting your precious time :8]. Thank you and all the others that tried to help! -- Seks, seksić, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa ) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne kształty... http://www.opera.com 007 |