<pedrocorreia.net ⁄>
<mySnippets order="rand" ⁄> <mySnippets order="rand" ⁄>

 
<myContacts ⁄> <myContacts ⁄>

<email ⁄>


pc@pedrocorreia.net

<windows live messenger ⁄>


pedrojacorreia@hotmail.com

<myCurriculum type="pdf" ⁄>


Download
 
<myBlog show="last" ⁄> <myBlog show="last" ⁄>

 
<myNews show="rand" ⁄> <myNews show="rand" ⁄>

 
<myNews type="cat" ⁄> <myNews type="cat" ⁄>

 
<myQuote order="random" ⁄> <myQuote order="random" ⁄>

 
<myPhoto order="random" ⁄> <myPhoto order="random" ⁄>

<pedrocorreia.net ⁄>
 
<myAdSense ⁄> <myAdSense ⁄>

 
<myVisitorsMap ⁄> <myVisitorsMap ⁄>

 
 

<Três operações sobre uma String ⁄ >




clicks: 2128 2128 2006-10-10 2006-10-10 goto mySnippets mySnippets php  Download  Bookmark This Bookmark This


Subscribe Snippets RSS  Subscribe Snippets Updates by E-mail


São daqueles que vêm do mundo do VB* e querem tão somente usar o left, right e mid para obter partes de uma string? Pois é, o PHP não tem estas funções, mas e que tal construirmos-as? É algo de muito simples e como vão poder observar não tem nada de extraordinário.

Senão vejamos...

  1. <?php
  2. /**
  3. * Obter os n 1os caracteres de uma string
  4. *
  5. * @param String
  6. * @param int - quantos caracteres?
  7. * @return String
  8. */
  9. function left ($str, $howManyCharsFromLeft){
  10. return substr ($str, 0, $howManyCharsFromLeft);
  11. }
  12.  
  13. /**
  14. * Obter os n últimos caracteres de uma string
  15. *
  16. * @param String
  17. * @param int - quantos caracteres?
  18. * @return String
  19. */
  20. function right ($str, $howManyCharsFromRight){
  21. return substr ($str, $strLen - $howManyCharsFromRight, strlen ($str));
  22. }
  23.  
  24.  
  25. /**
  26. * Obter uma parte intermediária da String
  27. *
  28. * @param String
  29. * @param int - começa em que posição? nota: a posição inicial é o 0
  30. * @param int - acaba em que posição?
  31. * @return String
  32. */
  33. function mid ($str, $startAt, $until){
  34. return substr ($str, $startAt, $until);
  35. }
  36.  
  37. //exemplos
  38. $str="Olá Mundo";
  39. echo left($str,3); //retorna 'Olá'
  40. echo right($str,5); //retorna 'Mundo'
  41. echo mid($str,1,6); // retorna 'lá Mun'
  42. //
  43. ?>



Quite easy, right?

Qualquer erro/ dúvida é só dizer!



Subscribe Snippets RSS  Subscribe Snippets Updates by E-mail





clicks: 2128 2128 2006-10-10 2006-10-10 goto mySnippets mySnippets php  Download  Bookmark This Bookmark This