文章教程

15.7上机实践

9/17/2020 9:26:54 PM 人评论 次浏览

15.7 上机实践

1.分别在字符串“PHP is the web scripting language of choice”和“PHP is the website scripting language of choice”中查找是否含有单独的单词web,进行表达式匹配。

【提示】


$string=preg_match("/\bweb\b/i","PHP is the web scripting language of choice.",$matches);//匹配

f($string==true){//判断匹配结果

print"A match was found.<br>";//输出“A match was found”

print_r($matches);//输出数组变量

echo"<br>";//换行

}else{//没有匹配上的情况

print"A match was not found.<br>";//输出“A match was not found”

print_r($matches);//输出数组变量

echo"<br>";//换行

}

$string=preg_match("/\bweb\b/i","PHP is the website scripting language of choice.",$matches);

if($string==true){//判断匹配结果

print"A match was found.";//输出“A match was found”

print_r($matches);//输出数组变量

echo"<br>";//换行

}else{//没有匹配上的情况

print"A match was not found.<br>";//输出“A match was not found”

print_r($matches);//输出数组变量

}


2.将字符串“The quick brown fox jumped over the lazy dog.”中的“quick”、“brown”、“fox”分别替换为“bear”、“black”、“slow”,然后将字符串逆序排列,再进行替换。

【提示】


$string="The quick brown fox jumped over the lazy dog.<br>";

$patterns[0]="/quick/";

$patterns[1]="/brown/";

$patterns[2]="/fox/";

$replacements[2]="bear";

$replacements[1]="black";

$replacements[0]="slow";

print preg_replace($patterns,$replacements,$string);

ksort($replacements);

print preg_replace($patterns,$replacements,$string);


教程类别