xxxemail:admin@admin.comimage:image">Grep.class.php xxxemail:admin@admin.comimage:image">Grep.class.php
搜尋
首頁後端開發php教程如何透過php 取得頁面中的指定內容類

功能:

1.取得內容中的url,email,image。

2.替換內容中的url,email,image。

url:url">xxx

email:admin@admin.com

image:image">

Grep.class.php

<?php
/** grep class
*   Date:   2013-06-15
*   Author: fdipzone
*   Ver:    1.0
*
*   Func:
*
*   set:        设置内容
*   get:        返回指定的内容
*   replace:    返回替换后的内容
*   get_pattern 根据type返回pattern
*/
class Grep{ // class start
    private $_pattern = array(
                            &#39;url&#39; => &#39;/<a.*?href="((http(s)?:\/\/).*?)".*?/si&#39;,
                            &#39;email&#39; => &#39;/([\w\-\.]+@[\w\-\.]+(\.\w+))/&#39;,
                            &#39;image&#39; => &#39;/<img .*?src=\"(http:\/\/.+\.(jpg|jpeg|gif|bmp|png))\" alt="如何透過php 取得頁面中的指定內容類" >/i&#39;
                        );
    private $_content = &#39;&#39;; // 源内容
    /* 設置搜尋的內容
    *  @param String $content
    */
    public function set($content=&#39;&#39;){
        $this->_content = $content;
    }
    /* 获取指定内容
    *  @param String $type
    *  @param int    $unique 0:all 1:unique
    *  @return Array
    */
    public function get($type=&#39;&#39;, $unique=0){
        $type = strtolower($type);
        if($this->_content==&#39;&#39; || !in_array($type, array_keys($this->_pattern))){
            return array();
        }
        $pattern = $this->get_pattern($type); // 获取pattern
        preg_match_all($pattern, $this->_content, $matches);
        return isset($matches[1])? ( $unique==0? $matches[1] : array_unique($matches[1]) ) : array();
    }
    /* 获取替换后的内容
    *  @param String $type
    *  @param String $callback
    *  @return String
    */
    public function replace($type=&#39;&#39;, $callback=&#39;&#39;){
        $type = strtolower($type);
        if($this->_content==&#39;&#39; || !in_array($type, array_keys($this->_pattern)) || $callback==&#39;&#39;){
            return $this->_content;
        }
        $pattern = $this->get_pattern($type);
        return preg_replace_callback($pattern, $callback, $this->_content);
    }
    /* 根据type获取pattern
    *  @param String $type
    *  @return String
    */
    private function get_pattern($type){
        return $this->_pattern[$type];
    }
} // class end
?>

#Demo

<?php
header(&#39;content-type:text/htm;charset=utf8&#39;);
require(&#39;Grep.class.php&#39;);
$content = file_get_contents(&#39;http://www.test.com/&#39;);
$obj = new Grep();
$obj->set($content);
$url = $obj->get(&#39;url&#39;, 0);
$email = $obj->get(&#39;email&#39;, 1);
$image = $obj->get(&#39;image&#39;, 1);
print_r($url);
print_r($email);
print_r($image);
$url_new = $obj->replace(&#39;url&#39;, &#39;replace_url&#39;);
echo $url_new;
function replace_url($matches){
    return isset($matches[1])? &#39;[url]&#39;.$matches[1].&#39;[/url]&#39; : &#39;&#39;;
}
?>

本文說明如何透過php 取得頁面中的指定內容類,更多相關內容請關注php中文網。

相關推薦:

介紹php相關語法技巧

#如何透過php 根據url自動產生縮圖

介紹php output_buffering 快取所使用的方法

#

以上是如何透過php 取得頁面中的指定內容類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何防止會話固定攻擊?如何防止會話固定攻擊?Apr 28, 2025 am 12:25 AM

防止會話固定攻擊的有效方法包括:1.在用戶登錄後重新生成會話ID;2.使用安全的會話ID生成算法;3.實施會話超時機制;4.使用HTTPS加密會話數據,這些措施能確保應用在面對會話固定攻擊時堅不可摧。

您如何實施無會話身份驗證?您如何實施無會話身份驗證?Apr 28, 2025 am 12:24 AM

實現無會話身份驗證可以通過使用JSONWebTokens(JWT)來實現,這是一種基於令牌的認證系統,所有的必要信息都存儲在令牌中,無需服務器端會話存儲。 1)使用JWT生成和驗證令牌,2)確保使用HTTPS防止令牌被截獲,3)在客戶端安全存儲令牌,4)在服務器端驗證令牌以防篡改,5)實現令牌撤銷機制,如使用短期訪問令牌和長期刷新令牌。

PHP會議有哪些常見的安全風險?PHP會議有哪些常見的安全風險?Apr 28, 2025 am 12:24 AM

PHP會話的安全風險主要包括會話劫持、會話固定、會話預測和會話中毒。 1.會話劫持可以通過使用HTTPS和保護cookie來防範。 2.會話固定可以通過在用戶登錄前重新生成會話ID來避免。 3.會話預測需要確保會話ID的隨機性和不可預測性。 4.會話中毒可以通過對會話數據進行驗證和過濾來預防。

您如何銷毀PHP會議?您如何銷毀PHP會議?Apr 28, 2025 am 12:16 AM

銷毀PHP會話需要先啟動會話,然後清除數據並銷毀會話文件。 1.使用session_start()啟動會話。 2.用session_unset()清除會話數據。 3.最後用session_destroy()銷毀會話文件,確保數據安全和資源釋放。

如何更改PHP中的默認會話保存路徑?如何更改PHP中的默認會話保存路徑?Apr 28, 2025 am 12:12 AM

如何改變PHP的默認會話保存路徑?可以通過以下步驟實現:在PHP腳本中使用session_save_path('/var/www/sessions');session_start();設置會話保存路徑。在php.ini文件中設置session.save_path="/var/www/sessions"來全局改變會話保存路徑。使用Memcached或Redis存儲會話數據,如ini_set('session.save_handler','memcached');ini_set(

您如何修改PHP會話中存儲的數據?您如何修改PHP會話中存儲的數據?Apr 27, 2025 am 12:23 AM

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然後使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

舉一個在PHP會話中存儲數組的示例。舉一個在PHP會話中存儲數組的示例。Apr 27, 2025 am 12:20 AM

在PHP會話中可以存儲數組。 1.啟動會話,使用session_start()。 2.創建數組並存儲在$_SESSION中。 3.通過$_SESSION檢索數組。 4.優化會話數據以提升性能。

垃圾收集如何用於PHP會議?垃圾收集如何用於PHP會議?Apr 27, 2025 am 12:19 AM

PHP會話垃圾回收通過概率機制觸發,清理過期會話數據。 1)配置文件中設置觸發概率和會話生命週期;2)可使用cron任務優化高負載應用;3)需平衡垃圾回收頻率與性能,避免數據丟失。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器