preg_match colon :

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 378
  • Loc: Canada

Post 3+ Months Ago

Hopefully a simple question.

I need to detect if a colon (:) is in a string

I need to use the negative as there must be no colon in order to do something:

If(!preg_match("/(:)+/",$strdomaine)
{
//If no colon found do something
}
else
{
//colon found
}

I have tried various regex versions but cannot seem to do it.

They say the colon is special, could be a delimiter, etc.
Does this mean there is a special way to detect it.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9193
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Just put a backslash in front of the colon, or in case you don't know exactly what to escape you could always run the string through preq_quote first and it will escape everything for you.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 378
  • Loc: Canada

Post 3+ Months Ago

One of our Ozzu members gave me the following which worked quite nice

Code: [ Select ]
/(\:)+/
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8489
  • Loc: USA

Post 3+ Months Ago

How about strpos? No need for regex here.

PHP Code: [ Select ]
<?php
 
if(strpos($strdomaine, ':') == false)
{
     // The colon was not found in the string
}
else
{
    // The colon WAS found in the string
}
?>
  1. <?php
  2.  
  3. if(strpos($strdomaine, ':') == false)
  4. {
  5.      // The colon was not found in the string
  6. }
  7. else
  8. {
  9.     // The colon WAS found in the string
  10. }
  11. ?>
  • spork
  • Brewmaster
  • Silver Member
  • spork
  • Posts: 6302
  • Loc: Seattle, WA

Post 3+ Months Ago

+1 for not using a regex just to detect the presence of a single character.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 47 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.