SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Changing é into html entities without touching the html elements ?

    Good day to you all,
    I'm working on a script which take the text from a txt file and add his content to an php page.
    I have no problem with that part, see code below :

    PHP Code:

    $textsec 
    file_get_contents('text.txt');
    echo 
    nl2br($textsec); 

    This page can include <b> <u>... html element.

    Where my problem start is that it's possible that I get some :
    PHP Code:
    &eacute;
    &
    agrave;

    ..
    words with accent.. 
    How can I convert them to their respective html entitie code without touching my html element ?

    Thanks!

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you need this? These elements should be displayed correctly.
    What encoding do you use? Are there only one encopding (language) on the page?

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try taking a look at strtr().

    PHP Code:
    echo strtr(
        
    "here is an <strong>é</strong>",
        array(
            
    'é'    => '&eacute;'
        
    )
    ); 
    #here is an <strong>&eacute;</strong> 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes you are right, I'm talking about them :

    &#233;
    &#234;
    ...

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anthony,
    can I do something like :

    PHP Code:

    echo nl2br(strtr(
        
    "here is an <strong>é</strong>",
        array(
            
    'é'    => '&eacute;'
            'ê'    
    => '&ecirc;'
        
    ))
    ); 

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what i'm now working but I'm getting an error:
    syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'

    PHP Code:

    $textsec 
    file_get_contents('constitution_fr.txt');
    echo 
    nl2br(strtr(
            
    $textsec,
            array(
            
    'é'    => '&eacute;'
            'ê'    
    => '&ecirc;'
            'è'    
    => '&egrave;'    
                    'È'    
    => '&Eagrave;'    
                    'É'    
    => '&Eacute;'    
                    'Ê'    
    => '&Ecirc;'    
                    'À'    
    => '&Agarve;'    
                    'Á'    
    => '&Aacute;'    
                    'Â'    
    => '&Acirc;'    
                    'Ç'    
    => '&Ccirc;'    
                    'Î'    
    => '&Icirc;'    
                    'Ï'    
    => '&Iuml;'    
                    'Ô'    
    => '&Ocirc;'    
                    'Ù'    
    => '&Uagrave;'    
                    'Û'    
    => '&Ucirc;'
                    'à'    
    => '&agrave;'
                    'á'    
    => '&aacute;'
                    'â'    
    => '&acirc;'
                    'ç'    
    => '&ccirc;'
                    'î'    
    => '&icirc;'
                    'ô'    
    => '&ocirc;'
                    'û'    
    => '&ucirc;'
        
    ));
                    ); 

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're on the wrong way.
    No need to replace it at all

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why ?

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand, it won't change the other no?

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice I got it working !!!
    Thanks!

    Is there a way to make that an external script that I load each time I need ?

  12. #12
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you need this replace?
    Why don't just output text as is? What's the problem?

  13. #13
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    Why do you need this replace?
    Why don't just output text as is? What's the problem?
    Most browsers will display all those characters as ? in the output unless you first convert them to the correct entity codes.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  14. #14
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's nonsense!
    Do you see it here as "?" in your browser?
    We're in the 21th century. Every browser in the world can display any encoding, even japanese Shift-JIS with no problem
    If it was properly set in HTTP header

    The only reason for replace is if you have two single-byte encodings on one page.
    But in this case you must use UTF for sure.


Bookmarks

Posting Permissions

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