以下例子提供了一种在标签之间检索文本的方法。
注意:不要使用正则表达式解析html。
通过使用正则表达式preg_match()、preg_match_all()函数,这二个函数进行的工作类似于php循环一样,多次遍历得到想要的结果。
另外,使用dom函数可以加快解析速度,得到干净的解析结果。
下面这个例子,使用 preg_match()函数来实现。
代码:
<?php
/**
*
* @get text between tags
*
* @param string (The string with tags)
*
* @param string $tagname (the name of the tag
*
* @return string (Text between tags)
*
*/
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>
注意:以上实现了一个简单的标签获取函数,不过它无法处理嵌套的标签,以及不完整的标签。
通过使用php的dom扩展,可以轻松解决这个问题。
来看下面的例子,函数本身有三个参考:
$tag
标签之间的文本
$html
要搜索的HTML或XML
$strict
告诉函数加载HTML或XML模式,默认为HTML模式。
第三个参数,允许设置函数来解析XML和一些XHTML文档中发现的自定义标签。
代码:
<?php
/**
*
* @get text between tags
*
* @param string $tag The tag name
*
* @param string $html The XML or XHTML string
*
* @param int $strict Whether to use strict mode
*
* @return array
*
*/
function getTextBetweenTags($tag, $html, $strict=0)
{
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
if($strict==1)
{
$dom->loadXML($html);
}
else
{
$dom->loadHTML($html);
}
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
/*** the array to return ***/
$out = array();
foreach ($content as $item)
{
/*** add node value to the out array ***/
$out[] = $item->nodeValue;
}
/*** return the results ***/
return $out;
}
?>
在这个例子中,如果使用普通的html,则无需提供第三个参数。
这允许处理无效的、不完整的html标签。缺少关闭的<p>标签,然而,使用dom和loadHtml可以允许这样的偏差。
这个例子仍然会解析html,并检索一个数组中所有文字之间的所有<a>锚标签。
代码:
<?php
$html = '<body>
<h1>Heading</h1>
<a href="http://"></a>
<p>paragraph here</p>
<p>Paragraph with a <a href="http://">LINK TO </a></p>
<p>This is a broken paragraph
</body>';
$content = getTextBetweenTags('a', $html);
foreach( $content as $item )
{
echo $item.'<br />';
}
?>
在最后的这个例子中,使用两个自定义标签,应用于XML或XHTML文件。
第三个参数被设置为使用XML模式和解析的自定义标签。
代码:
<?php
$xhtml = '<html>
<body>
<para>This is a paragraph</para>
<para>This is another paragraph</para>
</body>
</html>';
$content2 = getTextBetweenTags('para', $xhtml, 1);
foreach( $content2 as $item )
{
echo $item.'<br />';
}
?>
在php中,如果要统计文件的行数,对于小文件而言,使用file()函数是最方便的。
不过对于大文件而言,采用file()函数,就会效率很低,因为该函数会一次性把数据读取到一个数组中,然后储存在内存中。
由于php内存的限制,此方法处理大文件时,会效率极低,且容易出错。
本文介绍的这个方法,通过逐行遍历文件,可以处理任意大小的文件,而不用考虑内存限制的问题。
统计文件行数的代码,如下:
<?php
/*** 统计文件行数,调用示例 ***/
echo countLines("/path/to/file.txt");
/**
*
* @统计文件行数
* @param string filepath
* @return int
* @edit www.
*/
function countLines($filepath)
{
/*** open the file for reading ***/
$handle = fopen( $filepath, "r" );
/*** set a counter ***/
$count = 0;
/*** loop over the file ***/
while( fgets($handle) )
{
/*** increment the counter ***/
$count++;
}
/*** close the file ***/
fclose($handle);
/*** show the total ***/
return $count;
}
?>在php编程中,尤其是用户注册这样的模块时,对用户密码强度的要求,再多也不过。
现在很多网站,都会对密码强度进行检测,以获取符合安全要求的用户密码。
不过,有些对密码强度检测的功能,是建立在js或其它脚本上的,这可能会被恶意破坏者越过密码检测,而进行破坏。
本文介绍的这段代码,基于对长度、特殊字符、数字、字母等进行检测,另外,还可以自己添加一些额外的字符,以加强密码的安全性。
看代码吧,如下:
<?php
/**
*
* @检测密码强度
* @param string $password
* @return int
* @edit www.
*/
function testPassword($password)
{
if ( strlen( $password ) == 0 )
{
return 1;
}
$strength = 0;
/*** get the length of the password ***/
$length = strlen($password);
/*** check if password is not all lower case ***/
if(strtolower($password) != $password)
{
$strength += 1;
}
/*** check if password is not all upper case ***/
if(strtoupper($password) == $password)
{
$strength += 1;
}
/*** check string length is 8 -15 chars ***/
if($length >= 8 && $length <= 15)
{
$strength += 1;
}
/*** check if lenth is 16 - 35 chars ***/
if($length >= 16 && $length <=35)
{
$strength += 2;
}
/*** check if length greater than 35 chars ***/
if($length > 35)
{
$strength += 3;
}
/*** get the numbers in the password ***/
preg_match_all('/[0-9]/', $password, $numbers);
$strength += count($numbers[0]);
/*** check for special chars ***/
preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars);
$strength += sizeof($specialchars[0]);
/*** get the number of unique chars ***/
$chars = str_split($password);
$num_unique_chars = sizeof( array_unique($chars) );
$strength += $num_unique_chars * 2;
/*** strength is a number 1-10; ***/
$strength = $strength > 99 ? 99 : $strength;
$strength = floor($strength / 10 + 1);
return $strength;
}
/*** 调用示例 ***/
$password = 'php_tutorials_and_examples!123';
echo testPassword($password);
?>