• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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