Bluehost.com Web Hosting $6.95

converting string into array with regex

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-05-2003
Adam I Agnieszka Gasiorowski Fnord
 
Posts: n/a
Default converting string into array with regex


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
Reply With Quote
  #2 (permalink)  
Old 12-05-2003
Jason Wong
 
Posts: n/a
Default Re: [PHP] converting string into array with regex

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
*/
Reply With Quote
  #3 (permalink)  
Old 12-05-2003
Burhan Khalid
 
Posts: n/a
Default Re: [PHP] converting string into array with regex

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."
Reply With Quote
  #4 (permalink)  
Old 12-05-2003
Wouter Van Vliet
 
Posts: n/a
Default RE: [PHP] converting string into array with regex

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



Reply With Quote
  #5 (permalink)  
Old 12-05-2003
Adam I Agnieszka Gasiorowski Fnord
 
Posts: n/a
Default Re: [PHP] converting string into array with regex

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
Reply With Quote
  #6 (permalink)  
Old 12-05-2003
Cpt John W. Holmes
 
Posts: n/a
Default Re: [PHP] converting string into array with regex

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...
Reply With Quote
  #7 (permalink)  
Old 12-06-2003
Adam I Agnieszka Gasiorowski Fnord
 
Posts: n/a
Default Re: [PHP] converting string into array with regex

"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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:55 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0