Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2013, 03:12 AM   PM User | #1
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
preg_match from array

I've been trying for a while now to do this, but I can't quite get it right.

How can I write a function that has an array of a couple of words and then use preg_match to see if a variable starts with one of the words in the array?

This isn't right, but it might give an idea of what I mean.

PHP Code:
function prefix($prefix)
$pre = array('the''or');
$pre2 '/^[$pre]/';
if 
preg_match($pre2$prefix)
{

}
}

prefix($post); 
I also want to make case insensitive. /i

Last edited by nee; 02-06-2013 at 03:14 AM..
nee is offline   Reply With Quote
Old 02-06-2013, 10:59 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
first, do not use a character class, that would search for the occurrences of the letters, regardless of their order.
second, a set of matching words/search terms is separated by the PCRE OR operator (|).
PHP Code:
$search implode("|"$arr);
$pattern "#^($search)#i"
depending on what the function is supposed to return, even stripos() (in a loop) can do the job.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-06-2013, 05:52 PM   PM User | #3
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
Thanks. I ended upwith this as a test, and it works.

PHP Code:
function bad_prefix($prefix)
{
        
$arr = array('the''or');
        
$search implode("|"$arr);
        
$pattern "#^($search)#i";
        if (
preg_match($pattern$prefix))
       { echo 
"hello"; }
        

nee is offline   Reply With Quote
Old 02-09-2013, 06:34 AM   PM User | #4
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
I am still playing around with my booktitle function just seeing what I can make happen and what I can do to improve upon it to help me learn more.

Right now my function checks for the words 'the' and 'a' at the start of string, if either word is present then they are replaced with an empty string. The thing is that I don't want to type in the word 'thesis' and then simply have 'sis' displayed. So what I've done is added a space behind 'the' and 'a' so if the string is something like 'The red dog jumps" then 'the ' is taken away and only 'red dog jumps' is displayed, but if the string is something like 'Theodore Roosevelt was president' then it would display 'Theodore Roosevelt was president'.

Is there a better way to do this?

PHP Code:
$bookTitle trim(strip_tags($_POST['bookTitle'])); 

function 
book_title($prefix)
{
        
$words_front = array('the ''a ');
        
$search_front implode("|"$words_front);
        
$pattern_front "#^($search_front)#i";
        
        if (
preg_match($pattern_front$prefix))
       { 
       return 
preg_replace($pattern_front,'',$prefix);
       }
       else { echo 
$prefix; }
        
}

echo 
book_title($bookTitle); 
nee is offline   Reply With Quote
Old 02-09-2013, 08:52 AM   PM User | #5
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
You could use regular expressions word boundaries

PHP Code:
function book_title($title)
{
        
$words 'the|a';
        
$pattern "#^($words)\b#i";
        if (
preg_match($pattern$title))
                return 
preg_replace($pattern''$title);
        return 
$title;

gvre is offline   Reply With Quote
Old 02-09-2013, 09:49 AM   PM User | #6
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
Quote:
Originally Posted by gvre View Post
You could use regular expressions word boundaries

PHP Code:
function book_title($title)
{
        
$words 'the|a';
        
$pattern "#^($words)\b#i";
        if (
preg_match($pattern$title))
                return 
preg_replace($pattern''$title);
        return 
$title;

Was the extra return in the if-tatement a mis-type?
nee is offline   Reply With Quote
Old 02-09-2013, 09:51 AM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
why should it be a mis-type?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-09-2013, 09:54 AM   PM User | #8
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
What purpose does it serve? The function operates the same with or without right?

I mean I don't understand why there is

PHP Code:
return preg_replace($pattern''$title); 
and then

PHP Code:
return $title 
Wouldn't two returns be redundant and just need.

PHP Code:
return preg_replace($pattern''$title); 

Last edited by nee; 02-09-2013 at 10:03 AM..
nee is offline   Reply With Quote
Old 02-09-2013, 10:00 AM   PM User | #9
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
let’s rewrite the code a bit to make it more obvious.
PHP Code:
function book_title($title)
{
        
$words 'the|a';
        
$pattern "#^($words)\b#i";
        if (
preg_match($pattern$title)) {
                return 
preg_replace($pattern''$title);
        }
        else {
                return 
$title;
        }

but since the if() part has a return statement (and you can’t execute code after you reached a return), the explicit else is not necessary.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-09-2013, 10:04 AM   PM User | #10
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
let’s rewrite the code a bit to make it more obvious.
PHP Code:
function book_title($title)
{
        
$words 'the|a';
        
$pattern "#^($words)\b#i";
        if (
preg_match($pattern$title)) {
                return 
preg_replace($pattern''$title);
        }
        else {
                return 
$title;
        }

but since the if() part has a return statement (and you can’t execute code after you reached a return), the explicit else is not necessary.
Pretty neat, never knew that. I simply assumed since he renamed and tweaked some details he simply left the else out as well to emphasize the main part he was helping me with.

Last edited by nee; 02-09-2013 at 10:10 AM..
nee is offline   Reply With Quote
Old 02-09-2013, 02:40 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Dorm's conversion is slightly incorrect, but it does give the correct idea. It would be fine in this block since there is no other evaluation possible.
PHP Code:
        if (preg_match($pattern$title)) {
                return 
preg_replace($pattern''$title);
        }
        return 
$title
Would be the actual evaluation for it. The only difference is that with the conversion Dorm gave that second return would only function if the preg_match was false, whilst the actual code would return regardless of any other branch evaluations. Since this only have one branch, than it works just fine. Implicitly, it is an else for a single if branch.
The reason why is braces are NOT required in PHP, but only apply to the next instruction. I personally recommend not using this approach as it does create the potential for a modification blunder. It is valid in the language though, so to each their own.

Personally, I return once and only once from a function, and use logic to reassign. It cost me more in stack memory, but IMO it makes debugging slightly easier.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-09-2013, 06:06 PM   PM User | #12
nee
New Coder

 
Join Date: Feb 2013
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
nee is an unknown quantity at this point
Fou-lu, so you are saying you would do it like

PHP Code:
 if (preg_match($pattern$title)) {
                return 
preg_replace($pattern''$title);
        }
        return 
$title
or

PHP Code:
 if (preg_match($pattern$title)) {
                return 
preg_replace($pattern''$title);
        }
 else {
        return 
$title;
       } 
Wouldn't actually stating else be safer?

How would you personally rewrite the function so it only has one return? Just print or echo out the variable in the else statement?
nee is offline   Reply With Quote
Old 02-09-2013, 08:44 PM   PM User | #13
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,880
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by nee View Post
How would you personally rewrite the function so it only has one return? Just print or echo out the variable in the else statement?
neither. more like
PHP Code:
function book_title($title)
{
        
$words 'the|a';
        
$pattern "#^($words)\b#i";
        if (
preg_match($pattern$title)) {
                
$title preg_replace($pattern''$title);
        }
        
// no else because there’s nothing to change
        
return $title;

__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-10-2013, 08:16 AM   PM User | #14
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
The function could be rewritten without preg_match.

PHP Code:
function book_title($title)
{
        
$words 'the|a';
        
$pattern "#^($words)\b#i";
        return 
preg_replace($pattern''$title);

gvre is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 06:53 AM.


Advertisement
Log in to turn off these ads.