Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post

    Post Replace special chars not tags, php trigered by html

    Good day to you all,
    I working on a code which take a txt file and display it through php, but this thing is triggered by ajax.

    in the txt files there is html tags and html special chars.


    What I'm trying to do is encode only the special chars.
    ex : Ú Ŕ ý

    Php read those character not a problem, it when I pass it by ajax that i get some weird chars like squares with a question mark.

    Does anybody have an idea on how I can fix this ?


    Here is the code that get triggered when i click on a ajax link :
    PHP Code:

    <?php
    $textsectb 
    file_get_contents('dep_en_tb.txt');
    $textsechttb htmlentities($textsectb);
    echo 
    $textsechttb;
    ?>
    I only need to replace the special char not the html tags.

    Thanks!

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    In which encoding 'dep_en_tb.txt' is saved ? If it is e.g. utf-8, I think you could try the following:
    PHP Code:
    <?php
    $textsectb 
    file_get_contents('dep_en_tb.txt');
    header('Content-type: text/plain; charset=utf-8');
    echo 
    $textsechttb;
    ?>
    This code would not encode anything at all. Still if the document is transferred with the proper encoding, I think you should not get squares or question marks instead of the entities...

    Of course I supposed that you have been working with responseText (not responseXML) property of XMLHttpRequest object.

    You said you would not like to encode the HTML. Please notice, that in some tasks not encoding HTML could create certain security problems.

    Edit: if you really would like to actually encode the entities, you could do it like (please remove spaces from strings like '& #233;' - the system did not allow me to post otherwise converting entities to characters):
    PHP Code:
    $textsectb str_replace(array('Ú','Ŕ','ý'),array('& #233;','& #232;','& #236;'),$textsectb); // please remove spaces from strings like '& #233;'! 
    Please see this table for reference how entities could be encoded ...
    Last edited by SKDevelopment; 09-20-2009 at 06:08 PM.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I don't have an answer to this, but I wanted to
    post this link, as the discussion is interesting, and
    you might get some ideas about your problem:

    http://bytes.com/topic/php/answers/8...ial-characters

    You didn't mention if your special characters are based on foreign languages,
    or are they embedded RTF codes, like tabs, indents, etc.


  •  

    Posting Permissions

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