Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Wyrażenia regularne
Forum PHP.pl > Forum > Przedszkole
lDoran
Chciałbym wyciągnąć wartość value z tego ciągu:
Kod
input type="hidden" id="referredby" name="referredby" value="http://devldoran.wordpress.com/wp-admin/post-new.php"

mój kod:
  1. ...
  2. preg_match('/input type="hidden" id="referredby" name="referredby" value="(.*?)"/', $input, $output);
  3. $this->_aTokens['referredby'] = $output[1];
  4. ...

niestety indeks tablicy $output[1] jest pusty
Kod
array(2) { [0]=>  string(62) "input type="hidden" id="referredby" name="referredby" value=""" [1]=>  string(0) "" }

WTF?! sciana.gif
Fantazyn
Taki kod:
  1. $input = 'input type="hidden" id="referredby" name="referredby" value="http://devldoran.wordpress.com/wp-admin/post-new.php"';
  2. preg_match('/input type="hidden" id="referredby" name="referredby" value="(.*?)"/', $input, $output);
  3. var_dump($output);

wyświetla mi:
  1. 0 => string 'input type="hidden" id="referredby" name="referredby" value="http://devldoran.wordpress.com/wp-admin/post-new.php"' (length=114)
  2. 1 => string 'http://devldoran.wordpress.com/wp-admin/post-new.php' (length=52)


Więc to nie wina wyrażenia regularnego, tylko...

Porównaj u siebie zmienną output[1].

Dlaczego u Ciebie:
zmienna ma wartość: input type="hidden" id="referredby" name="referredby" value=""

Nie masz w niej wpisanej wartości value...

Edit: a możesz zrobić var_dump zmiennej $input przed wrzuceniem jej do prega?
lDoran
Kiedy na localhoscie pobieram stronę odpowiedzialną za dodanie postu w cms'ie wp wartość value jest pełna, przy wyciąganiu wartości preg_match $output[1] jest pusty nie rozumiem dlaczego tak się dzieje

EDIT:
  1. private function writeNewPost() {
  2. curl_setopt($this->_hCurl, CURLOPT_URL, $this->_sWebSite . 'wp-admin/post-new.php');
  3. $input = curl_exec($this->_hCurl);
  4. var_dump($input);
  5. $this->getTokens($input);
  6. curl_setopt($this->_hCurl, CURLOPT_POST, 1);
  7. curl_setopt($this->_hCurl, CURLOPT_POSTFIELDS,
  8. '_wpnonce=' . $this->_aTokens['_wpnonce'] .
  9. '&_wp_http_referer=' . $this->_aTokens['_wp_http_referer'] .
  10. '&user_ID=' . $this->_aTokens['user_ID'] .
  11. '&action=' . $this->_aTokens['hiddenaction'] .
  12. '&originalaction=' . $this->_aTokens['originalaction'] .
  13. '&post_author=' . $this->_aTokens['post_author'] .
  14. '&post_type=' . $this->_aTokens['post_type'] .
  15. '&original_post_status=' . $this->_aTokens['original_post_status'] . '
  16. &referredby=' . $this->_aTokens['referredby'] . '
  17. &_wp_original_http_referer=' . $this->_aTokens['_wp_original_http_referer'] .
  18. '&auto_draft=' . $this->_aTokens['auto_draft'] . '
  19. &post_ID=' . $this->_aTokens['post_ID'] .
  20. '&autosavenonce=' . $this->_aTokens['autosavenonce'] .
  21. '&meta-box-order-nonce=' . $this->_aTokens['meta-box-order-nonce'] .
  22. '&closedpostboxesnonce=' . $this->_aTokens['closedpostboxesnonce'] .
  23. '&wp-preview=
  24. &hidden_post_status=' . $this->_aTokens['hidden_post_status'] .
  25. '&post_status=draft
  26. &hidden_post_password=' . $this->_aTokens['hidden-post-password'] .
  27. '&hidden_post_visibility=' . $this->_aTokens['hidden-post-visibility'] . '
  28. &visibility=public
  29. &post_password=
  30. &jj=' . $this->_aTokens['jj'] .
  31. '&mm=' . $this->_aTokens['mm'] .
  32. '&aa=' . $this->_aTokens['aa'] .
  33. '&hh=' . $this->_aTokens['hh'] .
  34. '&mn=' . $this->_aTokens['mn'] .
  35. '&ss=' . $this->_aTokens['ss'] .
  36. '&hidden_mm=' . $this->_aTokens['hidden_mm'] .
  37. '&cur_mm=' . $this->_aTokens['cur_mm'] .
  38. '&hidden_jj=' . $this->_aTokens['hidden_jj'] .
  39. '&cur_jj=' . $this->_aTokens['cur_jj'] .
  40. '&hidden_aa=' . $this->_aTokens['hidden_aa'] .
  41. '&cur_aa=' . $this->_aTokens['cur_aa'] .
  42. '&hidden_hh=' . $this->_aTokens['hidden_hh'] .
  43. '&cur_hh=' . $this->_aTokens['cur_hh'] .
  44. '&hidden_mn=' . $this->_aTokens['hidden_mn'] .
  45. '&cur_mn=' . $this->_aTokens['cur_mn'] .
  46. '&original_publish=Opublikuj
  47. &publish=Opublikuj
  48. &post_category%5B%5D=0
  49. &newcategory=New+Category+Name
  50. &newcategory_parent=-1
  51. &_ajax_nonce-add-category=' . $this->_aTokens['_ajax_nonce-add-category'] . '
  52. &tax_input%5Bpost_tag%5D=
  53. &newtag%5Bpost_tag%5D=
  54. &post_title=' . $this->_sTitle .
  55. '&samplepermalinknonce=' . $this->_aTokens['samplepermalinknonce'] .
  56. '&content=' . $this->_sContent .
  57. '&excerpt=
  58. &trackback_url=
  59. &advanced_view=1
  60. &comment_status=open
  61. &ping_status=open
  62. &post_name=
  63. &wpl_enable_post_likes=1
  64. &wpl_like_status_hidden=1
  65. &wpl_enable_post_sharing=1
  66. &wpl_sharing_status_hidden=1');
  67. curl_setopt($this->_hCurl, CURLOPT_FOLLOWLOCATION, 1);
  68. curl_exec($this->_hCurl);

wynik var_dump
Kod
string(76213) "
Fantazyn
Rozumiem, że w $input masz całą stronę ściągniętą curl'em.
Sprawdź jak masz cierpliwość podgląd i poszukać samemu czy aby to czego szukasz pregiem jest podobne do
input type="hidden" id="referredby" name="referredby" value="(.*?)"

Jeżeli np. patrzysz w podglądzie strony to może się okazać, że parametry w źródle są w innej kolejności, np.
name="referredby" input type="hidden" id="referredby" value="(.*?)"

gdzieś pojawiłą się dodatkowa spacja, itp.

Spróbuj w Firefoxie (plugin Webdeveloper) wygenerować źródło i tam wyszukać żądanej frazy.

Albo ciąć wczęściej całą stronę (zmienną $input) na fragmenty korzytająć z funkcji strpos() i substr().

Pewnym rozwiązaniem jest też skorzystanie z zewnętrznej klasy: http://simplehtmldom.sourceforge.net/manual.htm

Powodzenia : )

Edit down: może wartość value jest dynamicznie dopisywana przez javascript...
lDoran
  1. <input type="hidden" id="referredby" name="referredby" value="" />

>.< curl nie pobiera wartości value angrysmiley.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.