Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]pregmatch
-preg_match-
post 9.02.2012, 18:58:01
Post #1





Goście







  1. $ile = preg_match_all('|(\d+)\-(\d+)|', $text, $matches);
  2. for ($i = 1; $i <= $ile; $i++)
  3. {
  4. preg_match('|(\d+)\-(\d+)|', $text, $matches);
  5. $number = $matches[1] - $matches[2];
  6. $text = str_replace($matches[0], $number, $text);
  7. }


Witam, mam skrypt który w stringu zamienia x-x, na x-x, czyli 5-2, na 3. A jak zrobić żeby brał też pod uwagę -x-x, próbuje dodać - z przodu, ale nic nie daje. :/
Go to the top of the page
+Quote Post
--k3dziu--
post 9.02.2012, 20:00:09
Post #2





Goście







Sam się jeszcze uczę korzystać z wyrażeń regularnych, ale spróbował zapisać tak (wersja długa):

  1. $ile = preg_match_all('/\-(\d+)\-(\d+)|(\d+)\-(\d+)/', $text, $matches);

znak |pośrodku wyrażenia to LUB

a w wersji krótkiej to tak:

$ile = preg_match_all('|.(\d+)\-(\d+)|', $text, $matches);
znak . na początku oznacza dowolny znak (dowolny znaczy że dowolny, także litery i inne co może powodować błedy)
Go to the top of the page
+Quote Post
-Gość-
post 9.02.2012, 23:22:49
Post #3





Goście







  1. $ile = preg_match_all('|.(\d+)\-(\d+)|', $text, $matches);
  2. for ($i = 1; $i <= $ile; $i++)
  3. {
  4. preg_match('|.(\d+)\-(\d+)|', $text, $matches);
  5. $number = $matches[1] - $matches[2];
  6. $text = str_replace($matches[0], $number, $text);
  7. }


Mam tak i dupa, -5-10, wychodzi -5, czyli nie czyta minusa. :/
Go to the top of the page
+Quote Post
vonski
post 10.02.2012, 09:25:31
Post #4





Grupa: Zarejestrowani
Postów: 292
Pomógł: 89
Dołączył: 27.12.2006
Skąd: Warszawa

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


Spróbuj tak:

  1. preg_match_all('/(-)?(\d+)-(\d+)/', $text, $matches, PREG_SET_ORDER);
  2.  
  3. foreach($matches as $submatches) {
  4. if($submatches[1] == '-') {
  5. $text = str_replace($submatches[0], -1 * $submatches[2] - $submatches[3], $text);
  6. } else {
  7. $text = str_replace($submatches[0], $submatches[2] - $submatches[3], $text);
  8. }
  9. }


--------------------
Zend Certified Engineer | Microsoft Certified Professional: Programming in HTML5 with JavaScript & CSS3 | Blog
Go to the top of the page
+Quote Post
-preg_match-
post 10.02.2012, 12:26:12
Post #5





Goście







Dzięki! Super, działa. Ale dlaczego jak zamienie -, na +, to nie działa -x+x? :<
Go to the top of the page
+Quote Post
vonski
post 10.02.2012, 14:21:28
Post #6





Grupa: Zarejestrowani
Postów: 292
Pomógł: 89
Dołączył: 27.12.2006
Skąd: Warszawa

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


  1. preg_match_all('/(-?\d+)([\-\+])(\d+)/', $text, $matches, PREG_SET_ORDER);
  2.  
  3. foreach($matches as $submatch) {
  4. if($submatch[2] == '-') {
  5. $text = str_replace($submatch[0], $submatch[1] - $submatch[3], $text);
  6. } else {
  7. $text = str_replace($submatch[0], $submatch[1] + $submatch[3], $text);
  8. }
  9. }


Teraz powinno działać dla "+" i "-"


--------------------
Zend Certified Engineer | Microsoft Certified Professional: Programming in HTML5 with JavaScript & CSS3 | Blog
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: 8.07.2025 - 08:56