Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 8, 2009, 10:28   #1
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
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!
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 10:34   #2
Shrapnel_N5
SitePoint Evangelist
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 480
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?
Shrapnel_N5 is online now   Reply With Quote
Old Nov 8, 2009, 10:36   #3
AnthonySterling
Previously, SilverBulletUK.
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 2,921
Try taking a look at strtr().

PHP Code:

echo strtr(
    
"here is an <strong>é</strong>",
    array(
        
'é'    => '&eacute;'
    
)
);
#here is an <strong>&eacute;</strong>
AnthonySterling is online now   Reply With Quote
Old Nov 8, 2009, 10:37   #4
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
yes you are right, I'm talking about them :

é
ê
...
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 10:46   #5
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
Anthony,
can I do something like :

PHP Code:


echo nl2br(strtr(
    
"here is an <strong>é</strong>",
    array(
        
'é'    => '&eacute;'
        'ê'    
=> '&ecirc;'
    
))
);
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 10:48   #6
AnthonySterling
Previously, SilverBulletUK.
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 2,921
Sure.
AnthonySterling is online now   Reply With Quote
Old Nov 8, 2009, 11:03   #7
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
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;'
    
));
                );
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 11:07   #8
Shrapnel_N5
SitePoint Evangelist
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 480
You're on the wrong way.
No need to replace it at all
Shrapnel_N5 is online now   Reply With Quote
Old Nov 8, 2009, 11:08   #9
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
why ?
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 11:09   #10
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
I don't understand, it won't change the other no?
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 11:17   #11
Peuplarchie
SitePoint Enthusiast
 
Join Date: Feb 2006
Posts: 60
Nice I got it working !!!
Thanks!

Is there a way to make that an external script that I load each time I need ?
Peuplarchie is offline   Reply With Quote
Old Nov 8, 2009, 11:24   #12
Shrapnel_N5
SitePoint Evangelist
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 480
Why do you need this replace?
Why don't just output text as is? What's the problem?
Shrapnel_N5 is online now   Reply With Quote
Old Nov 8, 2009, 13:06   #13
felgall
SitePoint Mentor
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 9,629
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.
felgall is online now   Reply With Quote
Old Nov 8, 2009, 13:10   #14
Shrapnel_N5
SitePoint Evangelist
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 480
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.
Shrapnel_N5 is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 11:39.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved