本文介绍了匹配精确字符串的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

29岁程序员,3月因学历无情被辞!

使用php时,匹配确切字符串的正则表达式是什么。

假设我们有文本:

Hello, world.

How are you today?

Today is sunshine and snow wouldn't you know.

如何使用正则表达式匹配字符串?:

sunshine and snow

推荐答案

使用PREG_MATCH:

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

使用strpos:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

这篇关于匹配精确字符串的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-15 22:06