1. #1
    iamdesign's Avatar
    Registered
    05/04/06
    Location
    Roeselare
    Posts
    177
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    0/0

    PHP: eregi_replace("<br>","\n", $stuff) vraagje

    Hoy allemaal,

    Klein vraagje (wel redelijk vervelend ) over eregi_replace("<br>","\n", $stuff).

    Ik heb dus een script geschreven dat je artikels over boek kan posten.

    Nu in mijn book_add.php doe ik het vlg om ook de opmaak mee te nemen in de database:

    PHP Code:
    // Functions to clean up html tags and secure for sql injections
                
    function formatStringForDatabase($string
                { 
                     if (
    get_magic_quotes_gpc() == 1
                     { 
                         return 
    htmlspecialchars(strip_tags($string));  
                     } 
                     else 
                     { 
                         return 
    addslashes(htmlspecialchars(strip_tags($string))); 
                     } 
                 }
            
                
    // Prepare variables
                
    $author formatStringForDatabase($author);
                
    $title formatStringForDatabase($title);
                
    $extraInfo eregi_replace("\n","<br>",$extraInfo); 
                
                
    $verzendExtraInfo formatStringForDatabase($extraInfo);
                
                
    // Make query
                
    mysql_query("INSERT INTO eann_book (author, title, extrainfo, photo) VALUES (\"$author\", \"$title\", \"$verzendExtraInfo\", \"$img_path\")") or die(mysql_error()); 
    Als ik in mijn database kijk word ook alles mooi met <br> afgebakend.

    Nu heb ik ook een functie "edit book" geschreven, zodat men stuff van het boek eventueel kan editen.

    Hierbij wordt de tekst opgehaald uit de DB, in een tekstarea gesmeten met deze code:

    PHP Code:
    // Result found => put everything in an array
                    
    $rij mysql_fetch_assoc($result);
                    
                    
    // Make more comfortable variables
                    
    $author $rij['author'];
                    
    $title $rij['title'];
                    
    $extraInfoOphaal stripslashes($rij['extrainfo']);
                    
                    
    $extraInfo eregi_replace("<br>","\n"$extraInfoOphaal); 
    Ook dit doet hij goed (dus in de textarea komt de stuff met de juiste opmaak)


    Maar nu als ik die tekst aanpas en weer op submit drukt, dan wordt er nix meer van de opmaak meegezonden ook al doe ik :

    PHP Code:
    // Functions to clean up html tags and secure for sql injections
                
    function formatStringForDatabase($string
                { 
                     if (
    get_magic_quotes_gpc() == 1
                     { 
                         return 
    htmlspecialchars(strip_tags($string));  
                     } 
                     else 
                     { 
                         return 
    addslashes(htmlspecialchars(strip_tags($string))); 
                     } 
                 }
            
                
    // Prepare variables
                
    $author formatStringForDatabase($author);
                
    $title formatStringForDatabase($title);
                
    $extraInfo eregi_replace("\n","<br>",$extraInfo); 
                
                
    $verzendExtraInfo formatStringForDatabase($extraInfo);
                
                
    // Make query
                
    $sql "UPDATE eann_book SET author='".$author."', title='".$title."', extrainfo='".$verzendExtraInfo."' WHERE bookid='".$bookID."'";
                
    mysql_query($sql) or die("There was a problem while processing your request, please contact the <a href='mailto:webmaster@eann.net'>webmaster</a>..."); 
    Wat compleet hetzelfde is als bij het adden...maar toch vertikt hij het van de \n's om te zetten naar <br>

    en dit sucked wel redeiljk zwaar

    is er iets dat ik over het hoofd zie???

    Bedankt alvast

    Laptop: Sony Vaio FE11 -:- 160gig -:- nVidia 7400 256MB -:- Duo Centrino @ 1.8Ghz

    Laptop: MB Pro 15" -:- 160gig -:- NVIDIA GeForce 8600M GT -:- 2.4Ghz
    no votes  

  2. #2
    omfg's Avatar
    Registered
    16/08/04
    Location
    9000
    Posts
    1,224
    iTrader
    4 (100%)
    Mentioned
    0 Post(s)
    waarom gebruik je niet gewoon nl2br ?
    There are two rules for success: 1. Never tell everything you know...
    no votes  

  3. #3
    iamdesign's Avatar
    Registered
    05/04/06
    Location
    Roeselare
    Posts
    177
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    0/0
    Ik heb mijn code om het in de database te steken veranderd naar

    PHP Code:
    // Replace newlines by <br />
                
    function nl2brStrict($text$replacement '<br />')
                {
                   return 
    preg_replace("((\r\n)+)"trim($replacement), $text);
                }
            
                
    // Prepare variables
                
    $author formatStringForDatabase($author);
                
    $title formatStringForDatabase($title);
                
    $verzendExtraInfo formatStringForDatabase($extraInfo);
                
                
    $verzendExtraInfo nl2brStrict($verzendExtraInfo); 
    Dit werkt alleszinds zie ik in mijn DB, maar alleen het ophale en in textarea steken lukt nog nie (hij zet nog steeds alles op dezelfde lijn) :@

    hiervoor gebruik ik vlg code

    PHP Code:
    //<br /> to newlines
                    
    function br2nl($text)
                    {
                           return  
    preg_replace('/<br\\s*?\/??>/i'''$text);
                    }
                    
                    
    // Make more comfortable variables
                    
    $author $rij['author'];
                    
    $title $rij['title'];
                    
    $extraInfoOphaal $rij['extrainfo'];
                    
                    
                    
    $extraInfo stripslashes($extraInfoOphaal);
                    
    $extraInfo br2nl($extraInfo); 
    als ik die

    return preg_replace('/<br\\s*?\/??>/i', '', $text);

    verander naar

    return preg_replace('/<br\\s*?\/??>/i', '\n', $text);

    Komen er allemaal \n'en in mijn textarea...wat wil zegge dat de preg_replace klopt...maja em doet het wel nie

    Laptop: Sony Vaio FE11 -:- 160gig -:- nVidia 7400 256MB -:- Duo Centrino @ 1.8Ghz

    Laptop: MB Pro 15" -:- 160gig -:- NVIDIA GeForce 8600M GT -:- 2.4Ghz
    no votes  

  4. #4
    Tyfius's Avatar
    Registered
    01/09/02
    Location
    Peutie
    Posts
    7,664
    iTrader
    0
    Mentioned
    4 Post(s)
    Reputation
    13/105
    Volgens mij heb je het jezelf nodeloos ingewikkeld gemaakt.
    Ikzelf werk zoals reeds aangehaald met een eenvoudiger nl2br(); om de data af te drukken waar nodig. In de database blijft die dan met \n zitten.
    Vanaf nu gaan we verder op BeyondGaming!
    In deze thread wordt uitgelegd hoe je jouw account kan migreren.
    no votes  

  5. #5
    iamdesign's Avatar
    Registered
    05/04/06
    Location
    Roeselare
    Posts
    177
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    0/0
    ja maar als ge da in een tekstarea afdrukt dan krijgde overal


    Code:
    hey<br />
    <br />
    dit<br />
    <br />
    zijn<br />
    <br />
    allemaal<br />
    <br />
    enters<br />
    <br />
    en das nu nie echt handig, dus moet ik iets hemme dat die <br />'s omzet in newline characters

    maar da vinnek dus nie

    EDIT: ok ik heb het kunnen oplossen...en het is echt ongelofelijk dat ik het heb gevonden, maar je moet dus bij die

    return preg_replace('/<br\\s*?\/??>/i', '\n', $text);

    doubele quotes gebruiken :|

    dit wordt dus
    return preg_replace('/<br\\s*?\/??>/i', "\n", $text);


    ..amai daar gaat de logica
    Last edited by iamdesign; 02-07-2006 at 19:34.

    Laptop: Sony Vaio FE11 -:- 160gig -:- nVidia 7400 256MB -:- Duo Centrino @ 1.8Ghz

    Laptop: MB Pro 15" -:- 160gig -:- NVIDIA GeForce 8600M GT -:- 2.4Ghz
    no votes  

  6. #6
    omfg's Avatar
    Registered
    16/08/04
    Location
    9000
    Posts
    1,224
    iTrader
    4 (100%)
    Mentioned
    0 Post(s)
    zet gewoon je inhoud in de database zonder enige nl2br of zoiets erop, en haal het er ook zo uit.
    in je textarea moet je er geen nl2br opzetten..
    There are two rules for success: 1. Never tell everything you know...
    no votes  

  7. #7
    iamdesign's Avatar
    Registered
    05/04/06
    Location
    Roeselare
    Posts
    177
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    0/0
    omfg...je hebt omfg gelijk

    en met smarty heb je nl2br modifier zie ik nu, ....sighhhhhhh zoveel tijd daar nu aan verspeeld :d
    ahja kzallet nie vergete nu

    Laptop: Sony Vaio FE11 -:- 160gig -:- nVidia 7400 256MB -:- Duo Centrino @ 1.8Ghz

    Laptop: MB Pro 15" -:- 160gig -:- NVIDIA GeForce 8600M GT -:- 2.4Ghz
    no votes  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in