一个搜索常用字符串的小脚本

时间:2022-03-24 08:52:08

/*

搜索出尚未完成。正则学好立马写。应该不难。2016-11-15 19:40 

更新:2016-11-24 18:55 完成正则部分

搜索功能待完善。加油!

*/

倘若搜索的内容存在就会弹出“匹配成功”

否则匹配失败。

正则区域:

/(trim)|(addslashes)|(stripslashes)|(stlen)|(substr)|(strcmp)|(strstr)|(substr_count)|(substr_replace)/

正则写的有点傻,如有好方法可加学习交流群:262017553告知。

不知道怎么更好的匹配,倘若这些是文字,而且是更尴尬的情况,估计这个正则就不行了。

 

 完整CODE区域:

<html>
<head>
<meta charset="utf-8"/>
<title>常用的字符串函数</title>
</head>
<body>
<h1>常用字符串函数手册_珍惜少年时</h1>
<form name="form1" action="" method="GET">
<input placeholder="请输入你要查找的函数" size="22" name="str"></input>
<input type="submit" value="sou"></input>
</form>
<style type="text/css">
<!--
.span{
color
:#f00;
font-size:25;
font
-weight:bolder;
}
-->
</style>
<?php
if(isset($_GET['str'])){
$str_keyword=$_GET['str'];
$reg="/(trim)|(addslashes)|(stripslashes)|(stlen)|(substr)|(strcmp)|(strstr)|(substr_count)|(substr_replace)/";
if(preg_match($reg,$str_keyword,$arr)){
echo "<script>alert('匹配成功')</script>";
}
else{
echo "<script>alert('匹配失败')</script>";
}
}
?>
<p>
&nbsp1.trim(string,charlist) :字符替换函数<br>
&nbsp&nbsp参数一:要修改的字符或者变量。<br>
&nbsp&nbsp参数二:要替换的字符。<br>
&nbsp&nbsp案例:$b=" ~xishaonian~ "<br>
&nbsp&nbspphpcode:echo trim($b,"~");输出结果是:
<?php
$b=" ~xishaonian~ ";
echo trim($b," ~");
?>
<br>
&nbsp&nbsp&nbsp&nbsp&nbsp#知识拓展延伸:<br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbspltrim以及rtim。顾名思义一个是左一个是右。
<br>
&nbsp2.addslashes(str) :反斜杠转义<br>
&nbsp&nbsp参数一:要转义的字符或者变量。<br>
&nbsp&nbsp案例:$a="hello 'w'orol" echo addslashes($a);输出的结果是:
<?php
$a="hello 'w'orld";
echo addslashes($a);
?>
<br>
&nbsp3.stripslashes(string) :去除反斜杠<br>
&nbsp&nbsp参数一:需要去除斜杠的字符串或变量<br>
&nbsp&nbsp案例:$a="xish\aonian" echo stripslashes($a);输出的结果是:
<?php
$a="xish\aonian";
echo stripslashes($a);
?>
<br>
&nbsp4.stlen(str) :字符串长度<br>
&nbsp&nbsp参数一:要计算长度的字符串或变量<br>
&nbsp&nbsp案例:$a="xishaoonian" echo strlen($a);输出的结果是:
<?php
$a="xishaonian";
echo strlen($a);
?>
<br>
&nbsp5.substr(string, start) :截取字符串<br>
&nbsp&nbsp参数一:字符串<br>
&nbsp&nbsp参数二:从第几个开始截取。<br>
&nbsp&nbsp参数三:[可选参数]截取的长度<br>
&nbsp&nbsp案例:$a="hello world" substr($a,6,2);输出的结果是:
<?php
$a="hello world";
echo substr($a,6,2);
?>
<br>
&nbsp6.strcmp(string1, string2):比较字符串,先比较首字符的ASCII大小,若首相同,再较第二位,类推;若两相等则返0,string1小于string2返回<0,string1大于string2返回>0 <br>
&nbsp&nbsp参数一:字符串1<br>
&nbsp&nbsp参数二:字符串2<br>
&nbsp&nbsp案例:$a="tools" $b="helloworld" strcmp($a,$b);输出的结果是:
<?php
$a="hool";
$b="helloworld";
echo strcmp($a, $b);
?>
<br>
&nbsp&nbsp&nbsp&nbsp&nbsp#知识拓展延伸:<br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp还有一个函数与之雷同,两函数只差在于大小写的区分,strcmp区分大小写,strnatcmp不区分大小写。

<br>
&nbsp7.strstr(haystack, needle) :检索字符串,即用来搜索<br>
&nbsp&nbsp参数一:被搜索的字符串<br>
&nbsp&nbsp参数二:所搜索的字符串<br>
&nbsp&nbsp案例:$a="xishaonian" echo strstr($a,"shao");输出的结果是:
<?php
$a="xishaonian";
echo strstr($a,"shao");
?>
<br>
&nbsp8.substr_count(string, start) :搜索字符出现的次数<br>
&nbsp&nbsp参数一:搜索的字符串或者变量<br>
&nbsp&nbsp参数二:搜索的内容<br>
&nbsp&nbsp案例:$a="xishaonian" echo substr_count($a,"n");输出的结果是:
<?php
$a="xishaonian";
echo substr_count($a, "n");
?>
<br>
&nbsp9.substr_replace(string, replacement, start) :替换字符串<br>
&nbsp&nbsp参数一:字符串<br>
&nbsp&nbsp参数二:要用什么去替换<br>
&nbsp&nbsp参数三:从第几位开水替换<br>
&nbsp&nbsp案例:$a="hello world" echo substr_replace($a,"珍惜少年时",6);输出的结果是:
<?php
$a="hello world";
echo substr_replace($a, "珍惜少年时", 6);
?>
<br>
&nbsp&nbsp&nbsp&nbsp&nbsp#知识拓展延伸:<br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp还有一个函数与之雷同,substr_ireplace不区分,substr_replace区分。
<br>
</body>
</html>

 

 

效果图如下:

一个搜索常用字符串的小脚本