Webmaster Forum


Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 10-11-2011, 05:16 PM
equityPHP's Avatar
Member
Latest Blog:
None

 
Join Date: 03-16-11
Posts: 41
iTrader: 0 / 0%
Regex question, maybe even easier

Here's the scenario. I want to check a string before inserting into the database.

What I am checking for is if the first 3 words of the string, match anything that's already in the database.

I am thinking that I can somehow you strpos but I'm not exactly sure how I would use it to make it work.

Someone with a little more knowledge clue me in on this one.

Thanks
__________________
My Klout | Follow Me
 
Reply With Quote
  #2 (permalink)  
Old 10-11-2011, 05:31 PM
equityPHP's Avatar
Member
Latest Blog:
None

 
Join Date: 03-16-11
Posts: 41
iTrader: 0 / 0%
Actually figured it out before I thought I would. Check this out.

Quote:
$title = "Test Post"; // Sometimes one might use test post 2
$exploded = explode(' ', $title);
$needles = array($exploded[0], $exploded[1], $exploded[2]);
foreach ($needles as $needle) {
echo $needle;
}
This hasn't been applied with mysql, but I'm doing that now so I can move forward.

Btw, just incase is wondering why I needed to do this.. Is because I have RSS Importers that sometimes bring in multiple posts across several networks that were mirrored to those both.. and I don't need duplicate entries, even if it's from different sites.
__________________
My Klout | Follow Me
 
Reply With Quote
  #3 (permalink)  
Old 10-12-2011, 12:08 AM
AppsGuy's Avatar
Junior Member
 
Join Date: 08-16-11
Location: Toronto, Canada
Posts: 5
iTrader: 0 / 0%
the way I would do it in VB.NET is that I would

MatchCollection mc = Regex.Match(inputString, "\b\w+\b")
If mc.Count >= 3 Then ....

mc(0).Value <--- word 1
mc(1).Value < ---- word 2
blah blah
....

inside the regex "\b" means a zero-width assertion that only matches on the WORD BOUNDARIES. Then we have at least one \w which means character inside the word (kind of not separator not punctuation char - smth like letters, digits or underscore) - \w+ means at least ONE or MORE. And then we have to put another "\b" so it does not stop in the middle of the word but rather goes on until the end of the word and consumes the whole word

And so it will return a collection on match objects and each Match.Value would contain one word.

If I am not mistaken, I think that PHP regex syntax is very similar, and there should be some methods that would return collection of matches, so this same approach could probably be ported to PHP.

Cheers,
Alex
 
Reply With Quote
  #4 (permalink)  
Old 10-12-2011, 02:28 AM
equityPHP's Avatar
Member
Latest Blog:
None

 
Join Date: 03-16-11
Posts: 41
iTrader: 0 / 0%
I appreciate that. I'm sure that the regex functionality would be quicker, but this concept does the trick. Thanks for you input though. Love to learn.
__________________
My Klout | Follow Me
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any regex experts? DrDave Coding Forum 4 08-29-2011 07:37 AM
Regex.. kos Coding Forum 3 01-19-2011 03:46 PM
problem with a javascript regex ThreeSwords Coding Forum 1 09-03-2010 02:59 PM
Quick Regex Help Leon Coding Forum 1 08-18-2009 02:06 PM
breaking apart a string for regex I like pie Coding Forum 3 11-14-2006 10:22 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 09:01 PM.
Powered by vBulletin
Copyright © 2000-2013 Jelsoft Enterprises Limited.
Copyright © 2003 - 2013 Escalate Media LP




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.