Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Ce parere aveti de viteza/ modul ...

Love Lies Bleeding - 2024

Cum sterg mails din Promotions

Vanzare cumparare fara transfer b...
 Receptie ciudata, in functie de t...

Donez medicamente renale ptr pisica

Ce componenta e asta si ce ziceti...

Dupa 20 ani de facultate, am uita...
 Mobile.de ofera imprumut de bani ...

problema test grila

Digi24 a disparut de pe TV Lg

Drept de proprietate intelectuala...
 Jante noi shitbox

Trinitas TV 4K

Dacia 1316 cu 6 usi ...

Frecventa modificata radio
 

Scoate cuvinte dintr-un text . Ajutor ....

- - - - -
  • Please log in to reply
21 replies to this topic

#19
OriginalCopy

OriginalCopy

    I'm harmful, fear me please! :))

  • Grup: Senior Members
  • Posts: 27,268
  • Înscris: 10.08.2006

View PostSaurian, on Feb 19 2009, 08:49, said:

atunci trimite un link de unde să mă pot documenta despre asta :)

the dragon book, sara golemon: extending and embedding php

View Postmeeesiatat, on Feb 19 2009, 04:41, said:

nu aia e problema . problema e cum sa fac sa imi afiseze fiecare cuvant din fisieru 'text' unu sub altu . ma poate ajuta careva ? ......

Iterezi stringul cu while si strtok() cum ti s-a arata mai sus, in bucla afisezi tokenul si ce intelegi tu prin "linie noua", "\r\n" sau "<br>"

#20
Saurian

Saurian

    Active Member

  • Grup: Banned
  • Posts: 1,280
  • Înscris: 02.09.2008

View PostOriginalCopy, on Feb 19 2009, 11:44, said:

the dragon book, sara golemon: extending and embedding php

Știu că-s rău și sper să nu primesc un warn pentru asta :P :D dar:

eu
I know is the bad place to put you this question but I couldn't find your mail address. I am from a far country called Romania ( I bet you don't have a clue about the country ) and posting on a php forum I asked what was the difference between explode () and strtok (). Which is better and why? The moderator from the php section gaved me a short explanation that strtok is better but why and all about it is to complex for a PHP novice like me. I insisted to give me a link at least where I could read an explanation and he told me "the dragon book, sara golemon: extending and embedding php". The book is to expensive for me and I don't know if I can find it in Ro. So, I thought I could ask the author about this: why is strtok better than explode? If you can explain to a PHP novice it will be great or at least some link where I couldn't learn. Thanks in advance! :)

sara golemon
"Better" is, as always, a relative term.

If we're talking raw CPU/Memory/etc, then yes... strtok() will sometimes use marginally less memory because it doesn't have to create an array structure to hold the elements. However, PHP's strtok() differs from C's strtok() in that it actually *does* copy the source string before tokenization, so with a moderately large string you can wind up using quite a bit MORE memory due to the copy, and a bit more CPU due to the repeated fcalls.

Now, it also matters GREATLY what you wind up doing with the results of tokenization on which one is "better". Personally, I tend to use explode() {or preg_split()} because the array of elements I get back is just that much more convenient to work with and any potential marginal increase in CPU/memory is well worth the readability of the resulting code.

Short version: strtok() *can* be "better" due to not having to create an array to hold the elements of the tokenization, or explode() *can* be better due to not having to copy the full string on the initial fcall. Go with what you find most readable and let PHP worry about making it work well.

P.S. - This probably would have gone better on one of my blog posts, see: blog.libssh2.org

:harhar:

#21
kr0k0dilu

kr0k0dilu

    tăran ofuscat

  • Grup: Senior Members
  • Posts: 2,513
  • Înscris: 12.06.2007

View PostOriginalCopy, on Feb 18 2009, 20:57, said:

Si explode() de unde crezi ca-si ia stringul, sau isi salveaza stringul, nu tot in RAM? :P

Nu. Deci ambele functii isi salveaza informatiile in RAM. Diferenta este ca strtok se serveste in mod secvential cu date pe masura ce citesti, deci face mai putin uz de tabelele cu simboluri (hash tables) din interiorul zend engine.

Ok, ok ... complicat de inteles fara putine cunostinte despre php internals sau in general despre compilatoare/parsere ... deci tot scriind am decis sa renunt la explicatii :D
Pur si simplu este asa: strtok() foloseste resursele mai econom B) (nu injecteaza date in runtime decat atunci cand este nevoie de ele - la cerere)

"strtok() este pentru explode() ceea ce XMLReader este pentru DOM" cam asta e cea mai buna analogie.

LE: o sa pun la cale un benchmark

Desi nu cunosc intimitatile PHP, si nici macar limbajul pe exterior, as vrea totusi sa surprind cateva nuante, care mie mi se par firesti in cadrul oricarui limbaj.

Daca ar trebui sa extrag toate cuvintele dintr-un sir extrem de lung, si sa le inmagazinez intr-un array de siruri, parca as prefera functia explode(), din motivul ca ar necesita un singur apel pe stiva (fata de o multitudine de apeluri ale functiei strtok()). Apoi, functia explode() foloseste exclusiv cod intern, rapid si optimizat, in timp ce blocul de cod scris de utilizator, si care foloseste functia strtok() ar fi mai lent, chiar si la operatia de "umplere" a vectorului de siruri (sistemul va efectua diverse verificari interne la fiecare element in parte, etc).

Daca, in schimb, ai avea nevoie doar de primele cuvinte dintr-un sir foarte lung, mi se pare limpede ca strtok() ar fi mult mai rapida, deoarece n-ar necesita procesarea intregului sir initial.

#22
OriginalCopy

OriginalCopy

    I'm harmful, fear me please! :))

  • Grup: Senior Members
  • Posts: 27,268
  • Înscris: 10.08.2006

View PostSaurian, on Feb 23 2009, 18:44, said:

Știu că-s rău și sper să nu primesc un warn pentru asta :P  :D dar:

eu
I know is the bad place to put you this question but I couldn't find your mail address. I am from a far country called Romania ( I bet you don't have a clue about the country ) and posting on a php forum I asked what was the difference between explode () and strtok (). Which is better and why? The moderator from the php section gaved me a short explanation that strtok is better but why and all about it is to complex for a PHP novice like me. I insisted to give me a link at least where I could read an explanation and he told me "the dragon book, sara golemon: extending and embedding php". The book is to expensive for me and I don't know if I can find it in Ro. So, I thought I could ask the author about this: why is strtok better than explode? If you can explain to a PHP novice it will be great or at least some link where I couldn't learn. Thanks in advance! :)

sara golemon
"Better" is, as always, a relative term.
 
 If we're talking raw CPU/Memory/etc, then yes... strtok() will sometimes use marginally less memory because it doesn't have to create an array structure to hold the elements. However, PHP's strtok() differs from C's strtok() in that it actually *does* copy the source string before tokenization, so with a moderately large string you can wind up using quite a bit MORE memory due to the copy, and a bit more CPU due to the repeated fcalls.
 
 Now, it also matters GREATLY what you wind up doing with the results of tokenization on which one is "better". Personally, I tend to use explode() {or preg_split()} because the array of elements I get back is just that much more convenient to work with and any potential marginal increase in CPU/memory is well worth the readability of the resulting code.
 
 Short version: strtok() *can* be "better" due to not having to create an array to hold the elements of the tokenization, or explode() *can* be better due to not having to copy the full string on the initial fcall. Go with what you find most readable and let PHP worry about making it work well.
 
 P.S. - This probably would have gone better on one of my blog posts, see: blog.libssh2.org

:harhar:
She's always so friendly with those willing to learn :P

PS: alea erau doua carti :D prima destul de renumita

LE: oricum sper ca ti-e evident ca ti-a raspuns si din madria personala de "php internal" :P

Quote

and let PHP worry about making it work well.
Diferenta este semnificativa la stringuri mari

Anunturi

Chirurgia spinală minim invazivă Chirurgia spinală minim invazivă

Chirurgia spinală minim invazivă oferă pacienților oportunitatea unui tratament eficient, permițându-le o recuperare ultra rapidă și nu în ultimul rând minimizând leziunile induse chirurgical.

Echipa noastră utilizează un spectru larg de tehnici minim invazive, din care enumerăm câteva: endoscopia cu variantele ei (transnazală, transtoracică, transmusculară, etc), microscopul operator, abordurile trans tubulare și nu în ultimul rând infiltrațiile la toate nivelurile coloanei vertebrale.

www.neurohope.ro

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate