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

Thread: convert to html

  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    257
    Thanks
    8
    Thanked 0 Times in 0 Posts

    convert to html

    I have the next code:

    PHP Code:
    $mail str_replace("á""á"$mail); 
    This replace all characters á in corresponding html code. But How I can replace all html characters without put each by each replace sentence?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you knwo the exact html characters you want to replace, u can store them up in an array, with their replacemnts in another array and then use preg_replace, this way:
    PHP Code:
    //patterns to match
    $pat[0] = "/@/";
    $pat[1] = "/á/";
    //and so on
    //then the replacements
    $rep[0] = "@";
    $rep[1] = "á";
    //Replace
    $mail preg_replace($pat$rep$mail); 
    if you however want all html special characters within the string mail to be automatically translated to the normal meaning, use this
    PHP Code:
    $mail html_entity_decode($mail); 
    hey, echo out $mail after u try this to be sure it gave u wat u are looking for.

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Quote Originally Posted by urgido
    I have the next code:

    PHP Code:
    $mail str_replace("á""á"$mail); 
    This replace all characters á in corresponding html code. But How I can replace all html characters without put each by each replace sentence?
    I guess you gave up on using htmlentities()? The third parameter of htnlentities allows you to choose a character set as well, if thats an issue....

    However, another way is to put all of your characters into an array, then iterate through the array, like so:

    PHP Code:
    $char_array = array('á' => 'á','replace2' => 'character2''replace3' => 'character3');
    foreach (
    $char_array as $key => $val)
    {
         
    $mail str_replace($val$key$mail);

    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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