PHP 正则过滤 html 标签、空格、换行符的代码 (文章格式化)

$str=preg_replace("/\s+/", " ", $str); //过滤多余回车 
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格) 

$str=preg_replace("/<\!--.*?-->/si","",$str); //注释 
$str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE 
$str=preg_replace("/<(\/?html.*?)>/si","",$str); //过滤html标签 
$str=preg_replace("/<(\/?head.*?)>/si","",$str); //过滤head标签 
$str=preg_replace("/<(\/?meta.*?)>/si","",$str); //过滤meta标签 
$str=preg_replace("/<(\/?body.*?)>/si","",$str); //过滤body标签 
$str=preg_replace("/<(\/?link.*?)>/si","",$str); //过滤link标签 
$str=preg_replace("/<(\/?form.*?)>/si","",$str); //过滤form标签 
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签 

$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str); //过滤applet标签 
$str=preg_replace("/<(\/?applet.*?)>/si","",$str); //过滤applet标签 

$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str); //过滤style标签 
$str=preg_replace("/<(\/?style.*?)>/si","",$str); //过滤style标签 

$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str); //过滤title标签 
$str=preg_replace("/<(\/?title.*?)>/si","",$str); //过滤title标签 

$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str); //过滤object标签 
$str=preg_replace("/<(\/?objec.*?)>/si","",$str); //过滤object标签 

$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str); //过滤noframes标签 
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str); //过滤noframes标签 

$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str); //过滤frame标签 
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str); //过滤frame标签 

$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str); //过滤script标签 
$str=preg_replace("/<(\/?script.*?)>/si","",$str); //过滤script标签 
$str=preg_replace("/javascript/si","Javascript",$str); //过滤script标签 
$str=preg_replace("/vbscript/si","Vbscript",$str); //过滤script标签 
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //过滤script标签 
$str=preg_replace("/&#/si","&#",$str); //过滤script标签,如javAsCript:alert(

 

做个记录 symfony/dom-crawler 使用入门

做个记录

symfony/dom-crawler 使用入门

> composer require "symfony/dom-crawler"
<?php
use Symfony\Component\DomCrawler\Crawler;
require 'vendor/autoload.php';

$html = <<<'HTML'
<!DOCTYPE html>
<html>
    <body>
        <p class="message">Hello World!</p>
        <p>Hello Crawler!</p>
    </body>
</html>
HTML;

$crawler = new Crawler($html);

foreach ($crawler as $domElement) {
    var_dump($domElement->nodeName);
}

http://symfony.com/doc/current/components/dom_crawler.html

https://segmentfault.com/q/1010000009019518

不使用 PHP 内置函数 使用方法写一个字符串反转的函数

<?php
function str_rev($str){
    for($i = 0;true;$i++){
        if(!isset($str[$i])){
            break;
        }
    }
    $return = "";
    for($j=$i-1;$j>=0;$j--){
        $return .= $str[$j];
    }
    return $return;
}
echo str_rev('abcdefg');

输出:

gfedcba

重点:字符串可以当数组来使用。

字符串中的字符可以通过一个以0为开始的,用类似数组结构中的方括号包含对应的数字来查找和修改,比如 $str[42], 可以把 字符串想像数组 。

跳出循环,使用 isset() 来判断。

不使用 array_merge() 实现多个数组的合并

<?php
function array_mer(){
    $return = [];
    $array = func_get_args();
    foreach($array as $arr){
        if(is_array($arr)){
            foreach($arr as $val){
                $return[] = $val;
            }
        }
    }
    return $return;
}
var_dump(array_mer([1],[1,2],[3,5]));

输出:

array(5) {
  [0]=>
  int(1)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
  [4]=>
  int(5)
}

重点:func_get_args() 动态获取参数的函数