Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] alternatywa strstr i jego 3 parametru
mefjiu
post 16.04.2008, 10:50:44
Post #1





Grupa: Zarejestrowani
Postów: 313
Pomógł: 5
Dołączył: 22.03.2005

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


Potrzebuje funkcję która która odetnę ciąg od 1 napotkanego znaku ale w lewo

Funkcja strstr() posiada optymalny 3 parametr
before_needle

If TRUE (the default is FALSE), strstr() returns the part of the haystack before the first occurence of the needle .

ale problem jest taki że:
5.3.0 Added the optional parameter before_needle .

jest coś innego ?

muszę uzyskać dosłownie coś identycznego
  1. <?php
  2. $user = strstr($email, '@', true);
  3. echo $user; // prints name
  4. ?>

ale odrazu pisze że nie jest mi to potrzebne do walidacji adresu email.

Proszę o wszelką pomoc

Ten post edytował mefjiu 16.04.2008, 11:29:44
Go to the top of the page
+Quote Post
nospor
post 16.04.2008, 10:55:07
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




http://pl2.php.net/manual/pl/function.strstr.php
zagladasz w komentarze i masz odpowiedniki


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
mefjiu
post 16.04.2008, 11:08:44
Post #3





Grupa: Zarejestrowani
Postów: 313
Pomógł: 5
Dołączył: 22.03.2005

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


no znalazłem, ale może jest jakaś inan funkcja która działa tak samo bo wstawianie tyle kodu to lekka przesada

  1. <?php
  2. // ==== I don't guarantee this is faster than the PHP 6 before needle, ====
  3. // ==== but it works for PHP below 6 atleast. ====
  4. // ==== IT ALSO HAS INCLUDE NEEDLE BOOLEAN.. ====
  5. function strstrbi($haystack,$needle,$before_needle,
  6. $include_needle,$case_sensitive)
  7. {
  8. $strstr = ($case_sensitive) ? 'strstr' : 'stristr';
  9. if($before_needle!=true && $before_needle!=false &&
  10. isset($before_needle)){
  11. die('PHP: Error in function '.chr(39).'$strstrbi'.
  12. chr(39).'
  13. : parameter '.
  14. chr(39).'$before_needle'.chr(39).' is
  15. not a supplied as a boolean.');
  16. } // END BOOLEAN CHECK '$before_needle'
  17. if($include_needle!=true && $include_needle!=false &&
  18.  isset($include_needle)){
  19. die('PHP: Error in function '.chr(39).'$strstrbi'.
  20. chr(39).' : parameter '.
  21. chr(39).'$include_needle'.chr(39).
  22. ' is not a supplied as a boolean.');
  23. } // END BOOLEAN CHECK '$include_needle'
  24. if($case_sensitive!=true && $case_sensitive!=false &&
  25.  isset($case_sensitive)){
  26. die('PHP: Error in function '.chr(39).'$strstrbi'
  27. .chr(39).' : parameter '.
  28. chr(39).'$case_sensitive'.chr(39).'
  29.  is not a supplied as a boolean.');
  30. } // END BOOLEAN CHECK '$case_sensitive'
  31. if(!isset($before_needle))
  32. {
  33. $before_needle=false;
  34. }
  35. if(!isset($include_needle))
  36. {
  37. $include_needle=true;
  38. }
  39. if(!isset($case_sensitive))
  40. {
  41. $case_sensitive=false;
  42. }
  43. switch($before_needle)
  44. {
  45. case true:
  46. switch($include_needle)
  47. {
  48. case true:
  49. $temp=strrev($haystack);
  50. $ret=strrev(substr($strstr($temp,$needle),0));
  51. break;
  52. // END case true : $include_needle
  53. case false:
  54. $temp=strrev($haystack);
  55. $ret=strrev(substr($strstr($temp,$needle),1));
  56. break;
  57. // END case false : $include_needle
  58. }
  59. break;
  60. // END case true : $before_needle
  61. case false:
  62. switch($include_needle)
  63. {
  64. case true:
  65. $ret=$strstr($haystack,$needle);
  66. break;
  67. // END case true: $include_needle
  68. case false:
  69. $ret=substr($strstr($haystack,$needle),1);
  70. break;
  71. // END case false: $include_needle
  72. }
  73. break;
  74. // END case false : $before_needle
  75. }
  76. if(!empty($ret)){
  77. return $ret;
  78. }else{
  79. return false;
  80. }
  81. }
  82. // === END FUNCTION 'strstrbi'
  83.  
  84. // Example
  85.  
  86. $email = 'user@example.com';
  87. $domain = strstrbi($email, '@', false, false, false);
  88. echo $domain; // prints example.com
  89.  
  90. $user = strstrbi($email, '@', true, false, false);
  91. echo $user; // prints user
  92. ?>
Go to the top of the page
+Quote Post
nospor
post 16.04.2008, 11:12:59
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




ja tam w komentarzach widzialem zdecydowanie krotsze wersje


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ayeo
post 16.04.2008, 11:18:55
Post #5





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


  1. <?
  2. $email = "kowalski@gmail.com";
  3. $new = substr($email, 0, strlen($email) - strlen(strstr($email,"@")));
  4.  
  5. echo $new;
  6. ?>


Ten post edytował ayeo 16.04.2008, 11:19:22


--------------------
Go to the top of the page
+Quote Post
nevt
post 16.04.2008, 11:24:41
Post #6





Grupa: Przyjaciele php.pl
Postów: 1 595
Pomógł: 282
Dołączył: 24.09.2007
Skąd: Reda, Pomorskie.

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


chyba nie te komentarze przeczytałeś, co trzeba
  1. <?php
  2. $email = 'test@xxx.com'
  3. $name = substr($email, 0, strpos($email, '@'));
  4. ?>


EDIT: a przy okazji - proszę kod w pierwszym poście ująć w odpowiednie znaczniki bbcode ...


--------------------

-
Oh no, my young coder. You will find that it is you who are mistaken, about a great many things... -
Go to the top of the page
+Quote Post
ayeo
post 16.04.2008, 11:26:42
Post #7





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


No, ja chyba też przesadziłem z tym :

  1. <?php
  2. $new = substr($email, 0, strlen($email) - strlen(strstr($email,"@")));
  3. ?>


Zupełnie zapomniałem o strpos();


--------------------
Go to the top of the page
+Quote Post
mefjiu
post 16.04.2008, 11:27:19
Post #8





Grupa: Zarejestrowani
Postów: 313
Pomógł: 5
Dołączył: 22.03.2005

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


no i ja znalazłem :-)

$data="/katalog/potem/asas/../asdsad";
$id=substr($data, 0 , strpos($data, "/../"));
echo $id;

teraz który najlepszy :-) i komu dać pomógł
nospor - podsunął pomysł (Kazał myśleć :-) )
ayeo - podał gotowca
nevt - podał gotowca

Teraz tylko niech ktoś powie który kod najlepszy

widzę że napisałem dokładnie to samo co nevt
W wiec "pomógł" dam dla nospor'a za naprowadzenie na trop

Ten post edytował mefjiu 16.04.2008, 11:29:12
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 10:30