1

How can I detect the dot at the beginning of ".net" with a preg_match_all pattern so I can also echo ".net" from the $skills list as well as the others keywords?

$skill = array(".net","software framework","microsoft");
$text = ".NET Framework is a software framework developed by Microsoft";

foreach ($skill as $skills) {
    preg_match_all("~\b$skills\b~i", $text, $matchWords);
    foreach ($matchWords[0] as $matchWord) {
        echo "<b>MatchWord:</b> " . $matchWord.  "<br>";
    }    
}      

Output (I'm Missing .NET): MatchWord: software framework MatchWord: Microsoft

executable
  • 3,365
  • 6
  • 24
  • 52
Seb
  • 83
  • 1
  • 10
  • The `.` isn't a literal `.` as your currently have it written. – user3783243 Dec 17 '18 at 15:55
  • I think https://stackoverflow.com/questions/6713310/how-to-specify-space-or-end-of-string-and-space-or-start-of-string will answer it. You also should use `preg_quote` though. https://3v4l.org/J8mBO – user3783243 Dec 17 '18 at 15:58
  • What exactly are you trying to do with `\b` which you can't do without it? Or what are you trying to do with your code at all and what are your challenges you get when you try to solve your problem/issue/task? – Progman Dec 17 '18 at 16:05

2 Answers2

0

There are two problems:

  1. ".net" in regular expressions means "a four letter word starting with any character then the characters n, e, t". That's because . is a special match-all operator.

  2. The problem is that using \b means "start next match after a word boundary", so in essence the \b is causing the the dot to be skipped.

One possible solution is to change .net to net in your $skills array, then allow matching of any word that starts with a dot by adding \.?: "~\b\.?$skills\b~i". In this version the \b is still there, so net won't match ASP.NET.

Notice that the dot is escaped (\.) due to it being a special "match all" operator in regular expressions.

Oz Solomon
  • 2,969
  • 23
  • 22
0

Got it:

$skill = array(".net","software framework","microsoft");
$text = ".NET Framework is a software framework developed by Microsoft";

foreach ($skill as $skills) {
preg_match_all("~(?<!\S)$skills(?!\S)~i", $text, $matchWords);
foreach ($matchWords[0] as $matchWord) {

     echo "<b>MatchWord:</b> " . $matchWord.  "<br>";
 }    
}

Output with.Net: MatchWord: .NET MatchWord: software framework MatchWord: Microsoft

Follow the link from user3783243 for more information.

Seb
  • 83
  • 1
  • 10