如何检查PHP中的字符串中是否存在特定字符?

如何检查PHP中的字符串中是否存在特定字符?,php,Php,我正在创建一个函数来检查字符串是否包含前缀为的特定字符,我需要根据两个条件验证文本 1.如果字符串包含前缀为+的字符,我必须在输出中显示不带前缀+的文本。 如: “我们今天吃晚饭” 输出: “我们今天吃晚饭” 2.如果字符串包含前缀为的字符,我必须在输出中显示文本,删除前缀为的整个文本。 如: “我们今天吃晚饭” 输出: “我们吃晚饭” 我将在此函数中传递一个名为length的额外参数,如果字符串长度小于给定的长度,则该字符串将被删除。 如: 长度=4; 如: “我们今天吃晚饭” 输出: “吃晚

我正在创建一个函数来检查字符串是否包含前缀为的特定字符,我需要根据两个条件验证文本

1.如果字符串包含前缀为+的字符,我必须在输出中显示不带前缀+的文本。 如: “我们今天吃晚饭” 输出: “我们今天吃晚饭”

2.如果字符串包含前缀为的字符,我必须在输出中显示文本,删除前缀为的整个文本。 如: “我们今天吃晚饭” 输出: “我们吃晚饭”

我将在此函数中传递一个名为length的额外参数,如果字符串长度小于给定的长度,则该字符串将被删除。 如: 长度=4; 如: “我们今天吃晚饭” 输出: “吃晚饭”。 如: “我们今天吃晚饭” 输出: “今天吃晚饭”。 到目前为止,我创建的函数是

$fulltext="-through the +use of the +tab +key";
$length=4;

    function checkstring($fulltext,$length)
    { 
      $stringArray = explode(" ", $fulltext);
     foreach ($stringArray as $value)
     {
        
     if(strlen($value) < $length)
     $fulltext= str_replace(" ".$value." " ," ",$fulltext);
     }
      return  $fulltext;
    }
    print_r(checkstring($fulltext,$length));

The output should be "use tab key"
$fulltext=“-通过+使用+选项卡+键”;
$length=4;
函数检查字符串($fulltext,$length)
{ 
$stringArray=explode(“,$fulltext);
foreach($stringArray作为$value)
{
if(strlen($value)<$length)
$fulltext=str_replace(“$value.”、“”、$fulltext);
}
返回$fulltext;
}
打印(检查字符串($fulltext,$length));
输出应为“使用制表键”
您可以使用此

if(ctype_alnum($fulltext)) {
  echo 'Does not contain symbols';
} else {
  echo 'Contains symbols';
}
你可以用这个

if(ctype_alnum($fulltext)) {
  echo 'Does not contain symbols';
} else {
  echo 'Contains symbols';
}

一种方法是使用正则表达式查找具有正确长度和前缀符号的匹配字符串。如果前缀等于“+”,则可以用不带前缀的字符串替换匹配项

请看下面的示例

/* a unix-style command line filter to convert uppercase
 * letters at the beginning of paragraphs to lowercase */
<?php
$line = "<p>Start of the paragraph</p>";
$line = preg_replace_callback(
    '|<p>\s*\w|',
    function ($matches) {
        return strtolower($matches[0]);
    },
    $line
);
echo $line;
?>
/*用于转换大写字母的unix风格命令行筛选器
*段落开头的字母改为小写*/

一种方法是使用正则表达式查找长度和前缀符号正确的匹配字符串。如果前缀等于“+”,则可以用不带前缀的字符串替换匹配项

请看下面的示例

/* a unix-style command line filter to convert uppercase
 * letters at the beginning of paragraphs to lowercase */
<?php
$line = "<p>Start of the paragraph</p>";
$line = preg_replace_callback(
    '|<p>\s*\w|',
    function ($matches) {
        return strtolower($matches[0]);
    },
    $line
);
echo $line;
?>
/*用于转换大写字母的unix风格命令行筛选器
*段落开头的字母改为小写*/

您可以对单词进行迭代,并检查是否已检查条件以保留在句子中

$fulltext = "-through the +use of the +tab +key";
$length = 4;

function checkstring($fulltext, $length)
{
    $words = preg_split('~\s~',$fulltext);

    $remains = [];

    foreach ($words as $word) 
    {
        if (strpos($word, '-') === 0) {
            continue;
        }

        if (strpos($word, '+') === 0) {
            $word = substr($word, 1);
        }
        elseif (strlen($word) <= $length) {
            continue;   
        }
        $remains[] = $word;
    }
    return trim(implode(' ', $remains));
}

echo checkstring($fulltext, $length);

查看。

您可以迭代这些单词,并检查是否已选中条件以保留在句子中

$fulltext = "-through the +use of the +tab +key";
$length = 4;

function checkstring($fulltext, $length)
{
    $words = preg_split('~\s~',$fulltext);

    $remains = [];

    foreach ($words as $word) 
    {
        if (strpos($word, '-') === 0) {
            continue;
        }

        if (strpos($word, '+') === 0) {
            $word = substr($word, 1);
        }
        elseif (strlen($word) <= $length) {
            continue;   
        }
        $remains[] = $word;
    }
    return trim(implode(' ', $remains));
}

echo checkstring($fulltext, $length);
查看。

函数checkstring($fulltext,$length)
{
$stringArray=explode(“,$fulltext);
foreach($stringArray作为$value){
如果($value[0]==“-”){
$fulltext=str_replace($value,“,$fulltext);
}如果($value[0]==“+”),则为else{
$fulltext=str_replace($value,substr($value,1),$fulltext);
}
if(strlen($value)<$length)
$fulltext=str_replace(“$value.”、“”、$fulltext);
}
返回$fulltext;
}
这是完整的函数

函数checkstring($fulltext,$length)
{
$stringArray=explode(“,$fulltext);
foreach($stringArray作为$value){
如果($value[0]==“-”){
$fulltext=str_replace($value,“,$fulltext);
}如果($value[0]==“+”),则为else{
$fulltext=str_replace($value,substr($value,1),$fulltext);
}
if(strlen($value)<$length)
$fulltext=str_replace(“$value.”、“”、$fulltext);
}
返回$fulltext;
}
下面是使用regex的完整函数

函数检查字符串(字符串$fulltext,int$length){
$matches=[];preg_match_all('/[+|-]([\w]+)/',$fulltext,$matches);
$text=”“;
if(isset($matches[1])&&count($matches[1])>0)
对于($i=0;$i使用正则表达式

函数检查字符串(字符串$fulltext,int$length){
$matches=[];preg_match_all('/[+|-]([\w]+)/',$fulltext,$matches);
$text=”“;
if(isset($matches[1])&&count($matches[1])>0)

对于($i=0;$i检查符号,但我需要检查一个条件,即如果+存在,我不需要删除字符串,而只需要将+替换为空;如果-存在,我需要删除整个字符串。此检查符号,但我需要检查一个条件,即如果+存在,我不需要删除字符串,而只需要将+替换为空,而如果-存在,我需要d要删除整个字符串,这对较少的行tq也是完全正确的。这对较少的行tq也是完全正确的