Jump to content

Recommended Posts

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';    

Link to comment
https://forums.phpfreaks.com/topic/119337-solved-help-with-patterns/
Share on other sites

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

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  :o

My bad.

 

Cheers,

 

NRG

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.