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 5 of 5
  1. #1
    DoA
    DoA is offline
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Partial text match in array?

    Hi guys,

    My array is as follows:

    Code:
    Array
    (
        [0] => http://www.arau.org/ct_home.php
        [1] => http://www.arau.org/
        [2] => http://en.wikipedia.org/wiki/Arau
    )
    All I want to do is look for certain partial matches & output their position in the array. It shouldn't care about the other results ... just the first match

    Code:
    Eg. $string_to_match = "arau.org"
    
    // If arau.org is found in array
    
    echo "Found at position 0";

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Running out the door for a few, so I'll keep this quick... let me know if any explanation is needed.
    PHP Code:
    function array_find( Array $array$needle )
    {
        foreach ( 
    $array as $key => $value )
        {
            if ( 
    FALSE !== strpos$value$needle ) )
            {
                
    $index $key;
                break;
            }
        }
        return isset( 
    $index ) ? $index FALSE;

    Usage:
    PHP Code:
    $links = array(
        
    => 'http://www.arau.org/ct_home.php',
        
    => 'http://www.arau.org',
        
    => 'http://en.wikipedia.org/wiki/Arau',
    );

    if ( 
    FALSE !== ( $index array_find$links'arau.org' ) ) )
    {
        echo 
    'Found at position '$index// Found at position 0
    }
    else
    {
        echo 
    'Not found';

    Last edited by kbluhm; 03-17-2010 at 12:27 AM.

  • Users who have thanked kbluhm for this post:

    DoA (03-17-2010)

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    PHP Code:
    $array = array( 'http://www.arau.org/ct_home.php','http://www.arau.org/','http://en.wikipedia.org/wiki/Arau');
     
     foreach(
    $array as $key => $value)
    {
         if(
    stristr($value"arau.org"))
        {
            
    $partial_match_key $key;
            break;
        }

    You can not say you know how to do something, until you can teach it to someone else.

  • Users who have thanked timgolding for this post:

    DoA (03-17-2010)

  • #4
    DoA
    DoA is offline
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Brilliant guys,

    Both work perfectly (you've no idea how long I've messed about with this.

    I'm using the second example as I can more easily see how it works.

    Thanks again.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP advises strpos() as faster and less memory intensive when compared to strstr().

    Tim's method uses stristr(), which makes the match case-insensitive. Compliment that with stripos() for a faster case-insensitive search.


  •  

    Posting Permissions

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