Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: wyrażenie regularne
Forum PHP.pl > Forum > PHP
sniver
Buduje coś jak wpisy na blipie, czyli jak ktoś wpisze: "właśnie programuje w #php" to tekst #php podlinkuje się jako tag do podstrony gdzie jest więcej takich postów które zawierają taki tag - w sumie niepotrzebnie tłumacze bo każdy to zna haha.gif

Ale jeśli ktoś wpisze: "właśnie programuje w #php i czytam post http://www.jakies.forum.pl/temat-123.html#post123", to analogicznie biorąc frazy #php i #post123 podlinkują się jako tagi, a ja chce aby podlinkował się cały url z ID posta czyli:http://www.jakies.forum.pl/temat-123.html#post123.

Napisałem wyrażenie regularne do adresów url:

$url = '{(http|https|ftp)(://)([a-zA-Z0-9\.-_/]*)}';
preg_match_all($url, $string, $arr);
..tu sie zamienia tekst na linki...

no i pora na te tagi:
$tags = '/#([a-zA-Z0-9\-_]*)([^a-zA-Z0-9\-_])/sUe';
$string = preg_replace($tags, 'self::tags(\'\\1\', \'\\2\')', $string);

w self::tags() zamieniają tagi na linki do podstrony z tagiem...

wiem że w tym ostatnim wyrażeniu regularnym mam błąd ale nie wiem co zrobić by mi to zadziałało tak jak chcę. Czy jest mi w stanie ktoś pomóc?
Zyx
Najprościej będzie, aby wyrażenie dzieliło tekst na tokeny bez zastanawiania się dokładnego, czemu to ma służyć. Weźmy podany przez Ciebie przykładowy ciąg:

Cytat
właśnie programuję w #php i czytam post http://www.jakies.forum.pl/temat-123.html#post123


Tokeny są tu następujące - ważne, aby otrzymać je w takiej kolejności, w jakiej występują w tekście:

1. właśnie programuję w - TEKST
2. #php - TAG_LUB_URL
3. i czytam post - TEKST
4. http://www.jakies.forum.pl/temat-123.html - URL
5. #post123 - TAG_LUB_URL

Uzyskaną listę tokenów skanujesz pętlą i każdy z nich odpowiednio parsujesz. Warto zapamiętać typ poprzedniego tokenu - w ten sposób łatwo rozpoznasz, czy #post123 jest tagiem czy częścią adresu. Po przeparsowaniu elementu doklejasz go do ciągu wyjściowego.

Zaleta takiego podejścia to możliwość stosunkowo łatwej rozbudowy oraz większe możliwości rozpoznawania oraz korekty błędów - wyrażenia regularne rozpoznają jedynie dość ograniczoną klasę języków i wiele sprawdzeń lub przekształceń po prostu nie da się zrealizować jedynie za ich pomocą.

sniver
OooO..w sumie to nie pomyślałem w ten sposób. Gdyby ktoś chciał gotową klase do tych pierdół to niech pisze na priv.
Do zamknięcia smile.gif
wookieb
Tokenizacja jest spoko ale wydaje mi się, że ten sam efekt można uzyskać w ten sposób
  1. $test = 'właśnie programuje w #php i czytam post <a href="http://www.jakies.forum.pl/temat-123.html#post123';" target="_blank">http://www.jakies.forum.pl/temat-123.html#post123';</a>
  2.  
  3. echo preg_replace('/([^\w])(#\w+)/', '$1TUTAJ_CIAG', $test);

Jeżeli się mylę to proszę poprawcie mnie
sniver
nieco inaczej to zrobiłem blinksmiley.gif

oto prościutki kodzik:

  1. <?php
  2.  
  3. class micropost {
  4.  
  5. static private $post = false;
  6.  
  7. static private $replace = false;
  8.  
  9. private function __construct( $string, $config=false ) {
  10. $this->post = array(
  11. 'string' => trim($string),
  12. 'toUser' => array(),
  13. 'tags' => array(),
  14. 'link' => array(),
  15. 'email' => array(),
  16. 'config' => $config
  17. );
  18.  
  19. }
  20.  
  21.  
  22.  
  23. public function parse( $string, $config=false ) {
  24. if( !self::$post ) {
  25. self::$post = new self( $string, $config );
  26. }
  27.  
  28.  
  29. $string = htmlspecialchars(strip_tags($string));
  30.  
  31. $email = '{([a-z0-9\.\-_]+@[a-z\.\-_]+\.[a-z]{2,4})}';
  32. preg_match_all($email, $string, $arr);
  33. $string = self::email( $arr[0], $string );
  34.  
  35. $gg = '{gg:([0-9]*)}';
  36. preg_match_all($gg, $string, $arr);
  37. $string = self::gg( $arr[1], $string );
  38.  
  39. $url = '{(http|https|ftp)(://)([a-zA-Z0-9\.-_/#]*)}';
  40. preg_match_all($url, $string, $arr);
  41. $string = self::url( $arr[0], $string );
  42.  
  43. $toUser = '/&gt;([a-zA-Z0-9\-_]*):/sUe';
  44. $string = preg_replace($toUser, 'self::toUser(\'\\1\')', $string);
  45.  
  46. $getUser = '/\^([a-zA-Z0-9\-_]*)([^a-zA-Z0-9\-_])/sUe';
  47. $string = preg_replace($getUser, 'self::getUser(\'\\1\', \'\\2\')', $string);
  48.  
  49. $tags = '/#([a-zA-Z0-9\-_]*)([^a-zA-Z0-9\-_])/sUe';
  50. $string = preg_replace($tags, 'self::tags(\'\\1\', \'\\2\')', $string);
  51.  
  52. return self::reparse($string);
  53.  
  54. }
  55.  
  56.  
  57. private function reparse( $string ) {
  58.  
  59. if( !self::$post ) {
  60. self::$post = new self( $string, $config );
  61. }
  62.  
  63. foreach( self::$post->replace as $key => $value ) {
  64. preg_match( '/^key(.*)_/', $key, $test );
  65. if( $test[1] == 'Email') {
  66. $url = '<a href="' . $value . '">[email]</a>';
  67. } elseif( $test[1] == 'Url') {
  68. $url = '<a href="' . $value . '">[link]</a>';
  69. } elseif( $test[1] == 'GG') {
  70. $url = '<a href="gg:' . $value . '">[gg]</a>';
  71. }
  72. $string = str_replace( $key, $url, $string );
  73. }
  74.  
  75. return $string;
  76.  
  77. }
  78.  
  79. private function toUser( $user ) {
  80.  
  81.  
  82. if( !isset(self::$post->post['config']['toUser']) ) {
  83. return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . $user . '">&gt;' . $user . ':</a>';
  84. } else {
  85. return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . $user . '" ' . self::$post->post['config']['toUser'] . '>&gt;' . $user . ':</a>';
  86. }
  87. }
  88.  
  89.  
  90. private function getUser( $user, $separator ) {
  91.  
  92. if( !self::$post ) {
  93. self::$post = new self( $string, $config );
  94. }
  95.  
  96. if( !isset(self::$post->post['config']['toUser']) ) {
  97. return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . mb_strtoupper($user) . '">^' . $user . '</a>' . $separator;
  98. } else {
  99. return '<a href="/uzytkownicy/' . $user . '.html" title="Zobacz profil użytkownika: ' . mb_strtoupper($user) . '" ' . self::$post->post['config']['toUser'] . '>^' . $user . '</a>' . $separator;
  100. }
  101.  
  102. }
  103.  
  104.  
  105. private function tags( $tag, $separator ) {
  106.  
  107. if( !self::$post ) {
  108. self::$post = new self( $string, $config );
  109. }
  110.  
  111. self::$post->post['tags'][] = $tag;
  112.  
  113. if( !isset(self::$post->post['config']['tags']) ) {
  114. return '<a href="/tagi/' . $tag . '.html" title="#' . mb_strtoupper($tag) . ' - statusy oznaczone tagiem ' . mb_strtoupper($tag) . '">#' . $tag . '</a>' . $separator;
  115. } else {
  116. return '<a href="/tagi/' . $tag . '.html" title="#' . mb_strtoupper($tag) . ' - statusy oznaczone tagiem ' . mb_strtoupper($tag) . '" ' . self::$post->post['config']['tags'] . '>#' . $tag . '</a>' . $separator;
  117. }
  118.  
  119. }
  120.  
  121.  
  122. private function email( $email, $string ) {
  123.  
  124. if( !self::$post ) {
  125. self::$post = new self( $string, $config );
  126. }
  127.  
  128. foreach( $email as $value ) {
  129. $key = 'keyEmail_' . md5($value);
  130. self::$post->replace[$key] = 'mailto:' . $value;
  131. $string = str_replace( $value, $key, $string );
  132. }
  133.  
  134. return $string;
  135.  
  136. }
  137.  
  138.  
  139.  
  140. private function url( $url, $string ) {
  141.  
  142. foreach( $url as $value ) {
  143. $key = 'keyUrl_' . md5($value);
  144. self::$post->replace[$key] = $value;
  145. $string = str_replace( $value, $key, $string );
  146. }
  147.  
  148. return $string;
  149.  
  150. }
  151.  
  152.  
  153. private function gg( $gg, $string ) {
  154.  
  155. foreach( $gg as $value ) {
  156. $key = 'keyGG_' . md5($value);
  157. self::$post->replace[$key] = $value;
  158. $string = str_replace( 'gg:' . $value, $key, $string );
  159. }
  160.  
  161. return $string;
  162.  
  163. }
  164.  
  165.  
  166. }
  167.  
  168.  
  169. $config = array(
  170. 'toUser' => 'style="font-weight: bold;"',
  171. 'link' => 'style="color: red;"',
  172. 'email' => 'style="color: yellow;"',
  173. 'tags' => 'style="color: #0ff;"'
  174. );
  175.  
  176. $post = '
  177. >sniver: oglądam #internet i czytam: <a href="http://www.youpc.pl/faq.html#blebleble" target="_blank">http://www.youpc.pl/faq.html#blebleble</a> napisz na: sniver@xyz.com lub gg:123, albo PW: ^sniver
  178. ';
  179.  
  180. echo trim(micropost::parse( $post, $config ));
  181.  
  182. ?>
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.