PHP
--
Questions
--
Followers
Top Experts
Hi E's,
I need a solution, to know if there are multiple dashes in a string.
I try to do like this, but no work:
$str = "--test---teste---";
if(preg_match('#(-\s*)+#i', $str)){
echo "contain";
}
//atention, "-test-test-" is correct!What I did wrong?
The best regards,
JC
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
If you are just looking to see if there are two dashes in a row, then use strpos():
if(strpos($str,"--") !== false)
{
echo "there are 2 dashes in a row in the string.";
}If you just want to check for at least one dash, use the same code but use "-" instead of "--".
If you want to count how many dashes there are in the string, use substr_count():
echo "There are " . substr_count($str,"-") . " dashes in the string.";The only reason to use a regular expression is if you need to check for spaces between the dashes.





EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
PHP
--
Questions
--
Followers
Top Experts
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.