Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> explode?
sneewee
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 14.05.2003
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Mam pewne pytanie...

nie moge znalezc w manualach ani nigdzie indziej, jak zrobic cos takiego..


input:

$string = "sKryPt testowy";

output:

$tablica[0] = "s";
$tablica[1] = "K";
$tablica[2] = "r";
$tablica[3] = "y";
$tablica[4] = "P";
$tablica[5] = "t";

---------

funkcja explode dzieli wylacznie po znakach, jak wiec podzielic tekst NA ZNAKI?


Pozdrawiam,
marcin at leliwa dot com
Go to the top of the page
+Quote Post
Jabol
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 467
Pomógł: 13
Dołączył: 22.02.2003

Ostrzeżenie: (0%)
-----


jest taka sztuczka jak umieszcazanie kodu php w BBcode - php!!
twój problem można rozwiązać bardzo łatwo[php:1:b1dfe1bdba]<?php
$str='jakis string';
print $str[0];
?>[/php:1:b1dfe1bdba]to da ci 'j', i tak dalej, bo string tak naprawde już jest tablicą znaków!!!
Go to the top of the page
+Quote Post
sneewee
post
Post #3





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 14.05.2003
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Cytat
jest taka sztuczka jak umieszcazanie kodu php w BBcode - php!!
twój problem można rozwiązać bardzo łatwo[php:1:2f092ee30b]<?php
$str='jakis string';
print $str[0];
?>[/php:1:2f092ee30b]to da ci 'j', i tak dalej, bo string tak naprawde już jest tablicą znaków!!!


nie za bardzo...
bo nie dziala [php:1:2f092ee30b]next($string)[/php:1:2f092ee30b]

rozwiazalem przez dodawanie znaku n pomiedzy znaki i dzieleniu explode po n

dzieki

?>[/php]
Go to the top of the page
+Quote Post
Jabol
post
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 467
Pomógł: 13
Dołączył: 22.02.2003

Ostrzeżenie: (0%)
-----


ale możesz zamiast next użyć for i używać $str[$i]
Go to the top of the page
+Quote Post
dragossani
post
Post #5





Grupa: Przyjaciele php.pl
Postów: 398
Pomógł: 0
Dołączył: --
Skąd: Poznań

Ostrzeżenie: (0%)
-----


[php:1:2143f03e94]<?php
function strtoarray($string)
{
$array=array();
for ($i = 0; $i <= strlen($string)-1; $i++) $array[]=$string{$i};
return $array;
}
?>[/php:1:2143f03e94]


--------------------
cease this long, long rest / wake and risk a foul weakness to live / when it all comes down / watch the smoke and bury the past again / sit and think what will come / raise your fears and cast them all away
Go to the top of the page
+Quote Post
Jabol
post
Post #6





Grupa: Przyjaciele php.pl
Postów: 1 467
Pomógł: 13
Dołączył: 22.02.2003

Ostrzeżenie: (0%)
-----


Cytat
[php:1:8f69c29479]<?php
function strtoarray($string)
{
   $array=array();
   for ($i = 0; $i <= strlen($string)-1; $i++) $array[]=$string{$i};
   return $array;
}
?>[/php:1:8f69c29479]
mam dwa pytania.
Po pierwsze, czemu <=strlen($string)-1, czy <strlen($string) nie byłoby wydajniejsze?
drugie, co oznacza zapis $str{$i};, dokładniej chodzi mi o { oraz }.
Go to the top of the page
+Quote Post
dragossani
post
Post #7





Grupa: Przyjaciele php.pl
Postów: 398
Pomógł: 0
Dołączył: --
Skąd: Poznań

Ostrzeżenie: (0%)
-----


Pierwsze pytanie - racja. To przez przeoczenie.
Drugie pytanie - { } to prawidłowe odwołanie się do stringa jak do tablicy. Twoje odwołanie w potaci [ ] działa tylko dlatego, że php niejawnie przeprowadza konwersję typów (na array).


--------------------
cease this long, long rest / wake and risk a foul weakness to live / when it all comes down / watch the smoke and bury the past again / sit and think what will come / raise your fears and cast them all away
Go to the top of the page
+Quote Post
DeyV
post
Post #8





Grupa: Zarząd
Postów: 2 277
Pomógł: 6
Dołączył: 27.12.2002
Skąd: Wołów/Wrocław




Jeszcze ciekawiej będzie w PHP5 - tam zostanie to zróżnicowane.
Cytat
a) There is an ambiguity in today’s sharing of array offsets syntax for both strings
and arrays. In code such as $str[0] = ‘a’; where $str is an empty string (due to
earlier php 4 and php 3 behavior) $str is treated as an array and therefore the
above statement would result in $str[0] being converted to an array and having its
offset 0 being set to the string “a”. However, some people would expect the result
to be $str as the string value “a”.
cool.gif By introducing a specialized syntax for string offsets it will be possible to
somewhat optimize the run-time processing, as we will know at compile-time that
the user specifically means to use a string offset.
c) Language wise it is much better if developers will be able to tell if the author
meant to use array offsets or string offsets in a code snippet.

Cytat
Functionality
The currently suggested syntax is as follows:
$str{2} = ‘a’;
An example of the new functionality:
$str1 = $str2 = “”;
$str1{0} = ‘a’;
$str2[0] = ‘a’;
The result will be $str1 being the string “a” and $str2 being array(0 => “a”).


--------------------
"Niezależnie od tego, jakie masz osiągnięcia, ktoś Ci pomaga..."
Go to the top of the page
+Quote Post
sneewee
post
Post #9





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 14.05.2003
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Cytat
ale możesz zamiast next użyć for i używać $str[$i]

Za duzo zadan juz tak zrobilem.
Mam 80 algorytmow do przerobienia do wtorku na mature. Wybralem php, ot takie zboczenie webmastera.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 23:33