<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >strtok</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="String" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="strstr" HREF="function.strstr.html"><LINK REL="NEXT" TITLE="strtolower" HREF="function.strtolower.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.strstr.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.strtolower.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.strtok" ></A >strtok</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN221768" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >strtok -- Tokeniza uma string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN221771" ></A ><H2 >Descrição</H2 >string <B CLASS="methodname" >strtok</B > ( string arg1, string arg2 )<BR ></BR ><P > <B CLASS="function" >strtok()</B > divide uma string (<CODE CLASS="parameter" >arg1</CODE >) em strings menores (tokens), com cada token sendo delimitado por qualquer caractere de <CODE CLASS="parameter" >arg2</CODE >. Quer dizer que, se você tem uma string como "Esta é uma string de exemplo" você poderia "tokenizá-la" em suas palavras individuais usando o caractere de espaço como delimitador do token. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN221786" ></A ><P ><B >Exemplo 1. <B CLASS="function" >strtok()</B > exemplo</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$string </font><font color="#007700">= </font><font color="#DD0000">"This is\tan example\nstring"</font><font color="#007700">;<br /></font><font color="#FF8000">/* Use tab e newline como caractere delimitador, que funciona bem */<br /></font><font color="#0000BB">$tok </font><font color="#007700">= </font><font color="#0000BB">strtok</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">,</font><font color="#DD0000">" \n\t"</font><font color="#007700">);<br />while (</font><font color="#0000BB">$tok</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Word=$tok<br>"</font><font color="#007700">;<br /> </font><font color="#0000BB">$tok </font><font color="#007700">= </font><font color="#0000BB">strtok</font><font color="#007700">(</font><font color="#DD0000">" \n\t"</font><font color="#007700">);<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Note que só a primeira chamada a strtok usa o argumento string. Cada chamada subseqüente a strtok só precisa do delimitador a ser usado, já que ele mantém o caminho de onde ele está na string atual. Para iniciar novamente, ou tokenizar uma nova string você simplesmente chama strtok com o argumento de string novamente para inicializá-la. Veja que você pode por múltiplos delimitadores como parâmetro. A string será tokenizada quando um dos caracteres no argumento são encontrados. </P ><P > O comportamento quando uma parte vazia foi encontrada mudou com o PHP 4.1.0. O antigo comportamento retornava uma string vazia, enquanto a nova, a correta, simplesmente pula a parte da string: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN221792" ></A ><P ><B >Exemplo 2. Comportamento antigo de <B CLASS="function" >strtok()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$first_token </font><font color="#007700">= </font><font color="#0000BB">strtok</font><font color="#007700">(</font><font color="#DD0000">'/something'</font><font color="#007700">, </font><font color="#DD0000">'/'</font><font color="#007700">);<br /></font><font color="#0000BB">$second_token </font><font color="#007700">= </font><font color="#0000BB">strtok</font><font color="#007700">(</font><font color="#DD0000">'/'</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump </font><font color="#007700">(</font><font color="#0000BB">$first_token</font><font color="#007700">, </font><font color="#0000BB">$second_token</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Output:<br /> string(0) ""<br /> string(9) "something"<br />*/ <br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN221796" ></A ><P ><B >Exemplo 3. Comportamento Novo <B CLASS="function" >strtok()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$first_token </font><font color="#007700">= </font><font color="#0000BB">strtok</font><font color="#007700">(</font><font color="#DD0000">'/something'</font><font color="#007700">, </font><font color="#DD0000">'/'</font><font color="#007700">);<br /></font><font color="#0000BB">$second_token </font><font color="#007700">= </font><font color="#0000BB">strtok</font><font color="#007700">(</font><font color="#DD0000">'/'</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump </font><font color="#007700">(</font><font color="#0000BB">$first_token</font><font color="#007700">, </font><font color="#0000BB">$second_token</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Output:<br /> string(9) "something"<br /> bool(false)<br />*/ <br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Também tome cuidado para que seus tokens não sejam iguais a "0". Isto avalia como <TT CLASS="constant" ><B >FALSE</B ></TT > em expressões condicionais. </P ><P > Veja também <A HREF="function.split.html" ><B CLASS="function" >split()</B ></A > e <A HREF="function.explode.html" ><B CLASS="function" >explode()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.strstr.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.strtolower.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >strstr</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >strtolower</TD ></TR ></TABLE ></DIV ></BODY ></HTML >