


Summary and sharing of regular expression methods for processing image addresses and img tags
这篇文章主要介绍了正则表达式处理图片地址、img标签的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
再上传表情或者图片地址时候很多时候不能直接上传<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >
,因此在上传评论或者图片之前应该先处理一下img标签。举例如下,希望可以帮助更多的程序猿~
<span style="font-size:14px;">//第一步是获取到带有img标签的字符串 var str = '图片1<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >图片2<img src="/static/imghwm/default1.png" data-src="arclist/em_23.gif" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >图片3<img src="/static/imghwm/default1.png" data-src="arclist/451.gif" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >';</span>
处理获取到的字符串
<span style="font-size:14px;">//将img标签替换为特定编码方式的表达式 var reg = /<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" [^ alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >]*src[=\"\'\s]+[^\.]*\/([^\.]+)\.[^\"\']+[\"\']?[^>]*>/gi; str = str.replace(reg, "[face:$1.gif]"); console.log(str);</span>
运算结果为:
<span style="font-size:14px;">图片1[face:sanai.gif]图片2[face:em_23.gif]图片3[face:451.gif]</span>
这样上传到服务器的代码就不会存在html标签,这是一种很好的应对带有图片评论且上传信息禁止html标签的方法。
接下来:
从服务器上返回的上一步中的运行结果,在反向解析,方法如下:
<span style="font-size:14px;">//将特定编码方式替换成img标签的表达式 var regg = / face:([\w]+).gif /gi; str = str.replace(regg,"<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" src='images/$1.gif' / alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >"); console.log(str);</span>
运行结果为:
<span style="font-size:14px;">图片1<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" src='images/sanai.gif' / alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >图片2<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" src='images/em_23.gif' / alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >图片3<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" src='images/451.gif' / alt="Summary and sharing of regular expression methods for processing image addresses and img tags" ></span>
下面附上代码的html页,可以全部复制粘贴到html文件中直接运行,亲测可用
<span style="font-size:14px;"><!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> var str = '图片1<img src="/static/imghwm/default1.png" data-src="arclist/sanai.png" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >图片2<img src="/static/imghwm/default1.png" data-src="arclist/em_23.gif" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >图片3<img src="/static/imghwm/default1.png" data-src="arclist/451.gif" class="lazy" alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >'; //将img标签替换为特定编码方式的表达式 var reg = /<img [^ alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >]*src[=\"\'\s]+[^\.]*\/([^\.]+)\.[^\"\']+[\"\']?[^>]*>/gi; str = str.replace(reg, "[face:$1.gif]"); console.log(str); //将特定编码方式替换成img标签的表达式 var regg = / face:([\w]+).gif /gi; str = str.replace(regg,"<img src='images/$1.gif' / alt="Summary and sharing of regular expression methods for processing image addresses and img tags" >"); console.log(str); </script> </body> </html></span>
以上两种方法很好的解决的对img标签的正反运算,第一种方法为之前收集,不记得原作者,如有冒犯可以提出署名。后一种方法为本人自写,存在一定的局限性,欢迎大家讨论~
The above is the detailed content of Summary and sharing of regular expression methods for processing image addresses and img tags. For more information, please follow other related articles on the PHP Chinese website!

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
