How to Check if String Contains Dash PHP?

03-Apr-2023

.

Admin

How to Check if String Contains Dash PHP?

Hi Dev,

Now, let's see post of How to Check if String Contains Dash PHP?. we will help you to give example of PHP Check Occurences of a Character Inside a String. you can understand a concept of Check if String Contains a Dash Character in PHP. step by step explain How do I Check if String Contains Dash PHP?.

There are to example to Check if String Contains Dash in PHP. in this example, we will use to preg_match(), str_contains() and strpos() function to Check if String Contains Dash. so Let's the following three example with output.

Example 1


index.php

<?php

// Check to see if string contains special characters (excluding dash)

$string = "Hi My Name is Vivek 1";

if(preg_match('/-/', $string)){

echo 'true';

}

else{

echo 'false';

}

?>

Output:

false

Example 2

index.php

<?php

// Check to see if string contains special characters (excluding dash)

$string = "Hi My Name is Vivek - 1";

if (str_contains($string, '-')) {

echo 'true';

}else{

echo 'false';

}

?>

Output:

true

Example 3

index.php

<?php

// Check to see if string contains special characters (excluding dash)

$string = "Hi My Name is Vivek 1";

if (strpos($string, '-') !== false) {

echo 'true';

}else{

echo 'false';

}

?>

Output:

false

I hope it could help you...

#PHP