php技术文章
- 用户与PHP、Javad、Salma 或
- 防止与应急负责人的机器人索引
- 防止语言广播台的显示
- 防止在语言上显示语言的语种
- 页: 1
- 允许在口头评论中增加超文本塔
- 2. 口头发言 Press URL
- 创造 Press Shortcodes
- 致言语使用者简介
- 预支和申请文件
- Add Menu Project to the WordPress Admin Menu
- 防止带PHP和Javales的寡妇
- 预支和改写文本
- • 如何消除言语
- Add a Submenu to the WordPress Admin Bar
- 防止利用No Captcha reCaptcha对WordPress的“博特攻击”
- 利用OMDocument将超文本改为PHP
- DOMDocument and UTF-8 Problem
- Add META Tags to WordPress Head
- POST 表格数据
- Phabricator Aphront Request / Save Error Fix
- 带有PHP定期表述的特别信
- • 如何规划长期吉大港山区
利用OMDocument将超文本改为PHP
// Formats post content for SSL function format_post_content($content = ) { $document = new DOMDocument(); // Ensure UTF-8 is respected by using mb_convert_encoding $document->loadHTML(mb_convert_encoding($content, HTML-ENTITIES , UTF-8 )); $tags = $document->getElementsByTagName( img ); foreach ($tags as $tag) { $tag->setAttribute( src , str_replace( http://davidwalsh.name , https://davidwalsh.name , $tag->getAttribute( src ) ) ); } return $document->saveHTML(); }In my example above, I find all
img
elements and replace their protocol with https://
. I will end up doing the same with iframe src
, a href
, and a few other rarely used tags. When my modifications are done, I call saveHTML
to get the new string.
Don t fall into the trap of trying to use regular expressions with HTML -- you re in for a future of failure. DOMDocument is pghtweight and will make your code infinitely more maintainable.