How To Check If A Specific Character Exists In A String In A PHP?

Viewed 208

Im creating a function to check whether a string contains a specific character prefixed,I need to validate the text based on two conditions.

1.if the string contains character + prefixed ,i have to show the text in output without prefix +. eg: "we have dinner +today" output: "we have dinner today"

2.if the string contains character - prefixed ,i have to show the text in output removing the whole text prefixed with -. eg: "we have dinner -today" output: "we have dinner".

I will pass an extra parameter called length in this function ,If the string length is less than the given length then the string will be removed. eg: length=4; eg: "we have dinner -today" output: "have dinner". eg: "we have dinner +today" output: "have dinner today". The function i have created so far is

$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"
5 Answers

You can use this

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

One way is to use regular expressions to find matching string with correct length and prefixed symbol. If the prefix equals '+' you can then replace the match with string without prefix.

Take a look at preg_replace_callback() and example below.

/* 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;
?>

You can iterate over the words and check if the conditions are checked to keep in the sentence.

$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);

Output :

use tab key

View the online demo.

function checkstring($fulltext, $length)
{
    $stringArray = explode(" ", $fulltext);
    foreach ($stringArray as $value) {

        if ($value[0] == "-") {
            $fulltext = str_replace($value, " ", $fulltext);
        } else if ($value[0] == "+") {
            $fulltext = str_replace($value, substr($value, 1), $fulltext);
        }
        if (strlen($value) < $length)
            $fulltext = str_replace(" " . $value . " ", " ", $fulltext);


    }
    return $fulltext;
}

Here is the complete function

Using regex

function checkstring(string $fulltext,int $length){
    $matches = []; preg_match_all('/[+|-](.[\w]+)/',$fulltext,$matches);
    $text = "";
    if(isset($matches[1]) && count($matches[1]) > 0)
        for($i=0;$i<count($matches[1]);$i++)
            if($matches[0][$i][0] == "+" && ($matches[0][$i][0] == "+" && strlen($matches[1][$i]) < $length))
               $text .= $matches[1][$i] . " ";
        
    return trim($text);
}

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

echo checkstring($fulltext,$length);

Output

use tab key
Related