tqla Posted August 12, 2008 Share Posted August 12, 2008 I am using the following code to show certain information based on whether is has a certain word in the title. The words are Productions, News, and Newsflash. The problem is that News and Newsflash both contain the word News and that screws things up. Is there a way to change this so that it needs to be exactly News or exactly Newsflash? Thanks. $pattern1 = '/^Productions/i'; $pattern2 = '/^News/i'; $pattern3 = '/^Newsflash/i'; Quote Link to comment Share on other sites More sharing options...
void Posted August 12, 2008 Share Posted August 12, 2008 if i understood right, try $pattern2 = '/^News$/i'; Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 12, 2008 Share Posted August 12, 2008 void tell him why your regular exspresion works............. brake it down.................. we all need to learn............... Quote Link to comment Share on other sites More sharing options...
tqla Posted August 12, 2008 Author Share Posted August 12, 2008 Thanks but that didn't work. Now News is not showing up at all. I should add that I am using preg_match. Quote Link to comment Share on other sites More sharing options...
void Posted August 12, 2008 Share Posted August 12, 2008 it really should work. maybe you could show us more code you have in there? Quote Link to comment Share on other sites More sharing options...
effigy Posted August 12, 2008 Share Posted August 12, 2008 What string are you matching this against? Word boundaries should suffice: /\bNews\b/. Quote Link to comment Share on other sites More sharing options...
tqla Posted August 12, 2008 Author Share Posted August 12, 2008 Yes boundaries worked. Thank you! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 12, 2008 Share Posted August 12, 2008 void tell him why your regular exspresion works............. brake it down.................. we all need to learn............... Going on Void's example: $pattern2 = '/^News$/i'; Basically, what is happening here is the expression starts with the circumflex character '^' which means, at the begining (start) of the subject (string to be searched for matches), find the letters N e w s consecutively and then we run into the '$' character which means the end of the subject. Finally, the 'i' outside the delimiters (/ characters) means that this search is case insensitive.. so whether the letters news is: News or news or nEws or neWs or NEWS, it will work. But this expression would fail if the string being searched had anything before (or after) the letters 'news', because of the circumflex (I think it can also be referred to as a Caret) and the dollarsign. In otherwords, the letters 'news' would have to be by itself in the string (nothing before or after it). so the string "This is a news update" would clearly fail. But as iffegy stated, by using word bounderies '\bnews\b, this expression would match the word news, regardless to whether the string just contained the word 'news', or if there was anything else before or after 'news'. Not sure if I am making any sense here. I would recommend reading the PHP manual segment on expressions here: http://www.php.net/manual/en/regexp.reference.php You can also goto: http://www.regular-expressions.info/reference.html (this might be easier to start with). Hope all this helps. Cheers, NRG Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 12, 2008 Share Posted August 12, 2008 so the string "This is a news update" would clearly fail. But as iffegy stated, by using word bounderies '\bnews\b, this expression would match the word news, regardless to whether the string just contained the word 'news', or if there was anything else before or after 'news'. I should correct myself here. The above sentence should read: so the string "This is a news update" would clearly fail. But as effigy stated, by using word bounderies '\bnews\b, this expression would match the word news, regardless to whether the string just contained the word 'news', or if there is any other word before or after 'news'. (word being the operative ..errm word here). The PHP manual describes what it constitues as a word boundery (think \w and \W). And effigy.. sorry I mispelled your name man My bad. Cheers, NRG Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.