Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] wycięcie kawałka tekstu z pliku
Forum PHP.pl > Forum > Przedszkole
modern
Witam

szukałem z wyrażeniami regularnymi, ale nie bardzo moge sie połapać...
kiedys widzialem tutaj na forum cos takiego ze mozna wyciągnąc z tekstu dany ciąg znaków i go przypisac jakies stalej ale nie moge tego znalezc

chodzi mi o to ze np. plik ps.out zawiera w sobie:

  1. PID TTY STAT TIME COMMAND
  2. 1 ? S 0:00 init [2]
  3. 2 ? S 0:00 [keventd]
  4. 3 ? SN 0:00 [ksoftirqd_CPU0]
  5. 4 ? S 1:05 [kswapd]
  6. 5 ? S 0:00 [bdflush]
  7. 6 ? S 0:00 [kupdated]
  8. 99 ? S 0:05 [kjournald]
  9. 505 ? S 0:00 [khubd]
  10. 594 ? Ss 0:17 dhclient -e -pf /var/run/dhclient.eth0.pid -lf /var/run/dhclient.eth0.leases eth0
  11. 600 ? Ss 0:00 /sbin/portmap
  12. 754 ? Ss 0:03 /sbin/syslogd
  13. 757 ? Ss 0:00 /sbin/klogd
  14. 804 ? Ss 0:00 /usr/sbin/exim4 -bd -q30m
  15. 810 ? Ss 0:00 /usr/sbin/inetd
  16. 821 ? Ss 0:01 /usr/sbin/sshd
  17. 826 ? Ss 0:00 /sbin/rpc.statd
  18. 829 ? Ss 0:00 /usr/sbin/atd
  19. 832 ? Ss 0:00 /usr/sbin/cron
  20. 849 tty1 Ss+ 0:00 -bash
  21. 850 tty2 Ss+ 0:00 /sbin/getty 38400 tty2
  22. 851 tty3 Ss+ 0:00 /sbin/getty 38400 tty3
  23. 852 tty4 Ss+ 0:00 /sbin/getty 38400 tty4
  24. 853 tty5 Ss+ 0:00 /sbin/getty 38400 tty5
  25. 854 tty6 Ss+ 0:00 /sbin/getty 38400 tty6
  26. 934 ? Ss 0:00 /usr/sbin/vsftpd
  27. 1577 ? Ss 0:00 /usr/sbin/named -u bind
  28. 1578 ? S 0:00 /usr/sbin/named -u bind
  29. 1579 ? S 0:00 /usr/sbin/named -u bind
  30. 1580 ? S 0:00 /usr/sbin/named -u bind
  31. 1581 ? S 0:00 /usr/sbin/named -u bind
  32. 4959 ? S 0:00 /bin/sh /usr/bin/mysqld_safe
  33. 4995 ? S 0:00 /bin/sh /usr/bin/mysqld_safe
  34. 4996 ? S 0:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  35. 4997 ? S 0:00 logger -p daemon.err -t mysqld_safe -i -t mysqld
  36. 5000 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  37. 5001 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  38. 5002 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  39. 5003 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  40. 5004 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  41. 5005 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  42. 5006 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  43. 5007 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  44. 5008 ? S 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
  45. 5184 ? Ss 0:00 SCREEN irssi
  46. 5185 pts/2 Ss+ 0:17 irssi
  47. 7255 ? S 0:01 /usr/sbin/apache
  48. 7446 ? Ss 0:00 SCREEN -m -d
  49. 7447 pts/3 Ss+ 0:00 /bin/sh
  50. 13392 ? S 0:43 ping onet.pl
  51. 13394 ? S 0:43 ping onet.pl
  52. 13433 ? S 0:44 ping onet.pl
  53. 14251 ? S 0:00 /bin/sh ./hlds_run -autoupdate -game cstrike +maxplayers 4 +map de_dust2 +ip 62.121.99.110 +port 27018 +exec server.cfg
  54. 14582 ? S 4:24 ./hlds_i686 -autoupdate -game cstrike +maxplayers 4 +map de_dust2 +ip 62.121.99.110 +port 27018 +exec server.cfg
  55. 14588 ? S 0:00 ./hlds_i686 -autoupdate -game cstrike +maxplayers 4 +map de_dust2 +ip 62.121.99.110 +port 27018 +exec server.cfg
  56. 14589 ? S 0:01 ./hlds_i686 -autoupdate -game cstrike +maxplayers 4 +map de_dust2 +ip 62.121.99.110 +port 27018 +exec server.cfg
  57. 15422 ? Ss 0:00 /usr/sbin/vsftpd
  58. 15423 ? S 0:33 /usr/sbin/vsftpd
  59. 16240 ? S 0:00 /usr/sbin/apache
  60. 16241 ? S 0:00 /usr/sbin/apache
  61. 16242 ? S 0:00 /usr/sbin/apache
  62. 16243 ? S 0:00 /usr/sbin/apache
  63. 16244 ? S 0:00 /usr/sbin/apache
  64. 16496 ? Ss 0:00 /usr/sbin/lpd -s
  65. 18783 ? Ss 0:00 sshd: modern [priv]
  66. 18785 ? S 0:00 sshd: modern [priv]
  67. 18787 ? S 0:00 sshd: modern@notty
  68. 18788 ? Ss 0:00 /usr/lib/sftp-server
  69. 18833 ? Ss 0:00 sshd: modern [priv]
  70. 18835 ? S 0:00 sshd: modern [priv]
  71. 18842 ? S 0:00 sshd: modern@pts/0
  72. 18843 pts/0 Ss+ 0:00 -bash
  73. 18907 ? S 0:00 /usr/sbin/apache
  74. 18941 ? R 0:00 ps ax


i chce wybrać z tego pliku wszystkie wiersze które mają w sobie to, a konkretnie tylko pierwszych 5 znaków czyli ich ID... tzn czasami jest tych znakó wiecej lub mniej... wiec moze wybrac od początku wierszu az do pierwszej spacji:
  1. ./hlds_i686 -autoupdate -game cstrike +maxplayers 4 +map de_dust2 +ip 62.121.99.110 +port 27018 +exec server.cfg


Probowalem ze strpos ale wiecej z tym zabawy niz efektów jakie by mnie zadowoliły sad.gif
Prosze o pomoc
mdco
Może tak:
  1. <?php
  2. $tmp = file('ps.out');
  3. foreach( $tmp as $v ) {
  4. if( preg_match('~^[s]*([0-9]+)s.+hlds_i686.+$~', $v, $matches) ) {
  5.  $res[] = $matches[1]; 
  6. }
  7. }
  8.  
  9. echo '<pre>';
  10. print_r($res);
  11. echo '</pre>';
  12. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.