Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Usuwanie znaków \r i \n
gcdreak
post
Post #1





Grupa: Zarejestrowani
Postów: 365
Pomógł: 8
Dołączył: 16.12.2008

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


Witam!
Do wprowadzania danych używam edytora napisanego pod jQuery http://code.google.com/p/lwrte/
Kiedy przesyłam tekst do bazy to zawiera on znaki powrotu karetki(\r) i nowej linii (\n).
Jak mogę się ich pozbyć?
Go to the top of the page
+Quote Post
wookieb
post
Post #2





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




[JAVASCRIPT] pobierz, plaintext
  1. tekst.replace(/\t|\n/g, '');
[JAVASCRIPT] pobierz, plaintext

Mniej więcej coś w tym stylu. Albo zrobić to z poziomu php (bezpieczniej). Twój wybór. Ale po co ci to robić?
Go to the top of the page
+Quote Post
gcdreak
post
Post #3





Grupa: Zarejestrowani
Postów: 365
Pomógł: 8
Dołączył: 16.12.2008

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


bo te znaki sie wyświetlają i na stronie:/
Go to the top of the page
+Quote Post
wookieb
post
Post #4





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Ale gdzie dokładniej się wyświetlają? textearea? div? pre?
Go to the top of the page
+Quote Post
szalik
post
Post #5





Grupa: Zarejestrowani
Postów: 25
Pomógł: 0
Dołączył: 28.06.2008

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


W php, pewnie pobierasz dane z bazy to użyj ereg_replace();
np.
  1. $text = ereg_replace("\n", "", $text);
Go to the top of the page
+Quote Post
kamil4u
post
Post #6





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


Taka uwaga (IMG:style_emoticons/default/smile.gif) :
Cytat
to użyj ereg_replace();

Użyj preg_replace, ponieważ:
Cytat
preg_replace(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace().


Ten post edytował kamil4u 17.08.2009, 15:34:02
Go to the top of the page
+Quote Post
gcdreak
post
Post #7





Grupa: Zarejestrowani
Postów: 365
Pomógł: 8
Dołączył: 16.12.2008

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


Cytat(wookieb @ 17.08.2009, 12:57:51 ) *
Ale gdzie dokładniej się wyświetlają? textearea? div? pre?


Wyświetlają się w divie.


@szalik && @kamil4u
Wolałbym uniknąć fitrowania danych, ten sposób to dla mnie ostateczność.
Go to the top of the page
+Quote Post
Fifi209
post
Post #8





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

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


Cytat(kamil4u @ 17.08.2009, 15:31:15 ) *
Taka uwaga (IMG:style_emoticons/default/smile.gif) :

Użyj preg_replace, ponieważ:


Taka uwaga:
Po co używać wyrażeń regularnych skoro php oferuje: trim ?
Go to the top of the page
+Quote Post
wookieb
post
Post #9





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




W takim razie w tym divie masz styl
Kod
white-space: pre;

Wystarczy ze to wywalisz i po kłopocie. Pokaż więcej kodu.

Trim usuwa tylko z początku i końca ciągu.

Ten post edytował wookieb 17.08.2009, 16:41:08
Go to the top of the page
+Quote Post
gcdreak
post
Post #10





Grupa: Zarejestrowani
Postów: 365
Pomógł: 8
Dołączył: 16.12.2008

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


Ja nie mam problemu ze spacjami tylko ze znakami takimi jak w temacie.

Jaki kod wam podać?
1. Strona na której się to wyświetla?(Jest be żadnych styli css)
2. Kod który dodaje tekst do bazy?
3. Kod który filtruje dane z formularza?
4. Kod strony na której wprowadzam dane?
Go to the top of the page
+Quote Post
wookieb
post
Post #11





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




3 ostatnie.
Go to the top of the page
+Quote Post
gcdreak
post
Post #12





Grupa: Zarejestrowani
Postów: 365
Pomógł: 8
Dołączył: 16.12.2008

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


ad 2:
  1. include_once('../classes/inputValidator.php');
  2.  
  3. $author = inputValidator::inputStrFilter($_POST['author']);
  4. $title = inputValidator::inputStrFilter($_POST['title']);
  5. $introduction = inputValidator::inputHtmlStrFilter($_POST['introduction']);
  6. $category = inputValidator::inputIntFilter($_POST['category']);
  7. $text = inputValidator::inputHtmlStrFilter($_POST['text']);
  8.  
  9. if(!inputValidator::validLogin($author))
  10. $errors = '<li>Write correct author.</li>';
  11. if(!inputValidator::validLogin($title))
  12. $errors .= '<li>Write correct title.</li>';
  13. if(!is_numeric($category))
  14. $errors .= '<li>Category is wrong.</li>';
  15. if(strlen($introduction) < 70)
  16. $errors .= '<li>Type introduction more than 70 chars.</li>';
  17. if(strlen($text) < 100)
  18. $errors .= '<li>Type content more than 100 chars.</li>';
  19.  
  20. if(strlen($errors) > 10){
  21. echo $errors;
  22. }
  23.  
  24. // All is OK
  25. include_once('../classes/article.php');
  26. $oArticle = new Article(new DB);
  27. $oArticle->addArticle($author, $title, $introduction, $category,$text);


ad 3:
  1. class inputValidator
  2. {
  3. public static function validEmail($email){
  4. if(preg_match('/^[a-zA-Z0-9ąćęśżźółń\._\-]+@[a-zA-Z0-9ąćęśżźółń_\.\-]{2,3}/', $email))
  5. return true;
  6. else
  7. return false;
  8. }
  9.  
  10.  
  11. public static function validLogin($login){
  12. if(strlen($login) > 5)
  13. return true;
  14. else
  15. return false;
  16. }
  17.  
  18.  
  19. public static function validPassword($password){
  20. if(strlen($password) > 5)
  21. return true;
  22. else
  23. return false;
  24. }
  25.  
  26.  
  27. public static function validUrl($url){
  28. if(preg_match('/^(ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal
    |www){1}(\.|:\/\/).{2,}\..{2,6}$/'
    , $url))
  29. return true;
  30. else
  31. return false;
  32. }
  33.  
  34. public static function inputStrFilter($value){
  35. return htmlspecialchars( strip_tags( trim($value)), ENT_QUOTES, 'UTF-8');
  36. }
  37.  
  38. public static function inputHtmlStrFilter($value){
  39. return htmlspecialchars(trim($value), ENT_QUOTES, 'UTF-8');
  40. }
  41.  
  42. public static function inputIntFilter($value){
  43. return intval($value);
  44. }
  45. }


ad4:
  1. <form id='formAddArticle' method="post" action="../controlers/addArticle.php">
  2. <p>
  3. <label for="author">Author:</label><input type="text" name="author" id="author"/>
  4. </p>
  5. <p>
  6. <label for="title">Title:</label><input type="text" name="title" id="titler"/>
  7. </p>
  8. <p>
  9. <label for="title">Category:</label>
  10. <select name="category">
  11. <?php while($category = $rCategory->fetch_object()): ?>
  12. <option value=" <?php echo $category->id; ?> "> <?php echo $category->name; ?> </option>
  13. <?php endwhile; ?>
  14. </select>
  15. </p>
  16. <p>
  17. <label for="introduction">Introduction:</label><textarea name="introduction" id="introduction" cols="100" rows="8"></textarea>
  18. </p>
  19. <p>
  20. <label for="text">Content:</label><textarea name="text" cols="100" rows="10" id="text" ></textarea>
  21. </p>
  22. <input type="submit" value="opublikuj"/>
  23. </form>

Do tego dochodzi wtyczka o której wcześniej pisałem. Kod jest zbyt długi żebym go tutaj wpisał, dlatego musicie go zobaczyć na stronie projektu(link w poprzednich postach)
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 7.10.2025 - 14:47