Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] string -> array
lunter
post
Post #1





Grupa: Zablokowani
Postów: 64
Pomógł: 0
Dołączył: 29.04.2004

Ostrzeżenie: (60%)
XXX--


Jak najszybciej i najprosciej ze stringa stworzyc tablice elementow jedno-bajtowych (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Przyklad:
$s='abcdefgh'; -> $a=array('a','b','c','d','e','f','g','h');


Probowalem tak jak w JS ( a=s.split('') ), czyli $a=explode('',$s). Niestety cos mi nie chce dzialac.

Czy da sie to zrobic bez uzywania 'for'/'while' (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Ten post edytował lunter 11.07.2005, 12:00:04
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Liko
post
Post #2





Grupa: Zarejestrowani
Postów: 436
Pomógł: 6
Dołączył: 8.11.2003
Skąd: Szczecin

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


Można też z PHP_Compact:
  1. <?php
  2. /**
  3.  * Replace str_split()
  4.  *
  5.  * @category php
  6.  * @package  PHP_Compat
  7.  * @link http://php.net/function.str_split
  8.  * @author Aidan Lister <aidan@php.net>
  9.  * @version  $Revision: 1.14 $
  10.  * @since  php 5
  11.  * @require  php 4.0.0 (user_error)
  12.  */
  13. if (!function_exists('str_split')) {
  14. function str_split($string, $split_length = 1)
  15. {
  16. if (!is_scalar($split_length)) {
  17. user_error('str_split() expects parameter 2 to be long, ' .
  18. gettype($split_length) . ' given', E_USER_WARNING);
  19. return false;
  20. }
  21.  
  22. $split_length = (int) $split_length;
  23. if ($split_length < 1) {
  24. user_error('str_split() The length of each segment must be greater than zero', E_USER_WARNING);
  25. return false;
  26. }
  27.  
  28. preg_match_all('/.{1,' . $split_length . '}/s', $string, $matches);
  29. return $matches[0];
  30. }
  31. }
  32. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Closed TopicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 7.10.2025 - 00:01