Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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"; }
            


  • #4
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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); 

  • #5
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    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;


  • #6
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 343 Times in 339 Posts
    why should it be a mis-type?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #13
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 343 Times in 339 Posts
    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;

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #14
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    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);



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •