Php, Regex question

Posted on 2011-05-09
Last Modified: 2012-05-11
Hi im trying to find all common wors and replace them
im doing that as followed:
$commonWords = array('is','that','them','and','he','the','\-');
        $text = preg_replace('/\b('.implode('|',$commonWords).')\b/','',$text);

I have a probel with finding the "-" sign and "--" and so on i did read on REGEX but it doesnt seem that those signs are a special chars.
Question by:Nura111
    LVL 10

    Expert Comment

    by:Tyler Laczko
    You do not need to "escape" the - character.

    $commonWords = array('is','that','them','and','he','the','-');

    Author Comment

    well I tried at the beginning to write it like that and its didnt work l it was still there at the output

    Author Comment

    LVL 3

    Accepted Solution

    You're absolutely correct with your initial code.
    The regex:

    Open in new window

    should work as you expect. Note though that you may want to specify the "Case Insensitive" option at the end (/i).

    Note also that the word boundaries include punctuation, so hyphenated words will still have the hyphen stripped. e.g "pre-processed" will become "preprocessed".

    There's a great little test app that can use on-line which also has a downloadable "air" version:

    I gave your regex a test and is definitely does what you're expecting.
    Hope this helps.

    Author Comment

    on is there a way to insert a pattern to check it? I dont seem to find it. Thank you beacuse it doesnt seem to work at my code. does it the same for 2 hay-fens (--)

    Author Comment

    LVL 3

    Assisted Solution

    Hi, sorry - timezone in AUS means I just got into work.
    The main sections of the program are:
     1. the Regular Expression string itself
     2. the options (these are in place of the extra parameters you add in PHP)
     3. the Replacement string
     4. the TEXT you wish to perform the test on
     5. the results in this case of the replacement
     6. Technical information regarding your RegEx and the capture groups.

    Remember, you need to select "REPLACE" rather than Match at the very top.

     Regex test

    Author Comment

    Thank you!!

    Author Comment

    where can you see the results if you replaced it?

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now