简体   繁体   中英

Convert ASCII Characters to HTML Numeric Code in PHP or Javascript

I'm looking to convert a string of ASCII characters (ie: a) to the html entities specifying ASCII codes they represent (ie: a;).

But this time instead of converting the entire HTML document it should just target the ASCII Characters Ignore the safe encoding characters (&, linebreaks, <, >, " and ') and html keywords like (class, div, h1, span etc)

So this should translate to something like

<div class="login-area-wrapper">
    <div class="login-area">
         <h1>Click here to Login</h1>
             <span class="create-account-link" id="createAccountSpan">
                    No Account?
                    <a href="https://www.sample.com/signup/" id="createAccount">Sign up</a>

THIS:

<div class="&#108;&#111;&#103;&#105;&#110;&#45;&#97;&#114;&#101;&#97;&#45;&#119;&#114;&#97;&#112;&#112;&#101;&#114;">
    <div class="&#108;&#111;&#103;&#105;&#110;&#45;&#97;&#114;&#101;&#97;">
         <h1>&#67;&#108;&#105;&#99;&#107;&#32;&#104;&#101;&#114;&#101;&#32;&#116;&#111;&#32;&#76;&#111;&#103;&#105;&#110;</h1>
             <span class="&#99;&#114;&#101;&#97;&#116;&#101;&#45;&#97;&#99;&#99;&#111;&#117;&#110;&#116;&#45;&#108;&#105;&#110;&#107;" id="&#99;&#114;&#101;&#97;&#116;&#101;&#65;&#99;&#99;&#111;&#117;&#110;&#116;&#83;&#112;&#97;&#110;">
                    &#78;&#111;&#32;&#65;&#99;&#99;&#111;&#117;&#110;&#116;&#63;
                    <a href="&#104;&#116;&#116;&#112;&#115;&#58;&#47;&#47;&#119;&#119;&#119;&#46;&#115;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#47;&#115;&#105;&#103;&#110;&#117;&#112;&#47;" id="&#99;&#114;&#101;&#97;&#116;&#101;&#65;&#99;&#99;&#111;&#117;&#110;&#116;">&#83;&#105;&#103;&#110;&#32;&#117;&#112;</a>

I Hope my explanation isn't that miserable...

Looks pretty crazy to me. :) First I'd write a regex for it, try https://regexr.com/ type in your code and find the right expression that matches what you're looking for. For instance: "([^"]*)" this would find everything in the quotes, but you probably need to add a regex tag to your question to get someone more qualified to figure it out exactly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM