[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Php, Regex question

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=strtolower($text);
        $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.
Thanks
0
Nura111
Asked:
Nura111
  • 6
  • 2
2 Solutions
 
Tyler LaczkoCommented:
You do not need to "escape" the - character.


$commonWords = array('is','that','them','and','he','the','-');
0
 
Nura111Author Commented:
well I tried at the beginning to write it like that and its didnt work l it was still there at the output
0
 
Nura111Author Commented:
anyone?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
double_helixCommented:
You're absolutely correct with your initial code.
The regex:
/\b(is|that|them|and|he|the|-)\b/i

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:
http://www.gskinner.com/RegExr/

I gave your regex a test and is definitely does what you're expecting.
Hope this helps.
0
 
Nura111Author Commented:
on http://www.gskinner.com/RegExr/ 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 (--)
0
 
Nura111Author Commented:
?
0
 
double_helixCommented:
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
0
 
Nura111Author Commented:
Thank you!!
0
 
Nura111Author Commented:
where can you see the results if you replaced it?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now