TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-03-2009, 03:12 AM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default searching array

Hey guys/gals,

I am trying to output only the items in the array that have ISBN in it. I tried both array_search() and in_array() functions and each time I got errors for using invalid data types in the second field.

Code:
<?php

$test = array('Wfjheighe','ISBN:454656','ISBN:9090943');


    foreach ($test as $id) {
    
    	
      if(array_search('ISBN',$id)) echo $id . "<BR />";
    
    }


?>

AND


<?php

$test = array('Wfjheighe','ISBN:454656','ISBN:9090943');


    foreach ($test as $id) {
    
    	
      if(in_array('ISBN',$id)) echo $id . "<BR />";
    
    }


?>
I basically just want ISBN:454656 and ISBN:9090943 to be outputted...

What am I doing wrong?
allworknoplay is offline  
Reply With Quote
Old 05-03-2009, 03:32 AM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

PHP Code:
<?php

$test 
= array('Wfjheighe','ISBN:454656','ISBN:9090943');
$term '/ISBN:(.*?)/Us';

foreach( 
$test as $id )
{
    if( 
preg_match($term$id$match) )
    {
        echo 
$match[0], '<br />';
    }
}

?>
This'll output what you want, how ever for future references some pointers.

when using echo to output data please the comma instead of the period to concatenate strings it is faster.

when you're processing strings without variables in them use single quotes as PHP doesn't process them.

Unless I'm mistaken standards mandate html be lowercase so <BR /> should <br />

P.S - I am not sure this would be the best method.
Enfernikus is offline  
Reply With Quote
Old 05-03-2009, 05:01 AM   #3 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

You are using in_array incorrectly in the second sample code; You are iterating over a block of elements and pass the second parameter to in_array() as a string where it would expect an array, further though the first parameter for in_array() does not work as a wildcard, it will see if theres an element with that content in this case ISBN and nothing else, meaning it will always return false here.

What you need to do is to use something like str[i]str():
PHP Code:
...
if(
stristr($id'ISBN') !== false)
{
    ...
}
... 
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 05-03-2009, 09:41 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

If you want to go the regular expression route, there's another function which is much friendlier to arrays: preg_grep.

Example
PHP Code:
$test  = array('Wfjheighe','ISBN:454656','ISBN:9090943');
$isbns preg_grep('/^ISBN:\d+$/Di'$test);
print_r($isbns); 
Output
Code:
Array
(
    [1] => ISBN:454656
    [2] => ISBN:9090943
)
Salathe is offline  
Reply With Quote
Old 05-03-2009, 02:18 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Enfernikus View Post


This'll output what you want, how ever for future references some pointers.

when using echo to output data please the comma instead of the period to concatenate strings it is faster.

when you're processing strings without variables in them use single quotes as PHP doesn't process them.

Unless I'm mistaken standards mandate html be lowercase so <BR /> should <br />

P.S - I am not sure this would be the best method.
Thanks, I had no idea that you can use commas to concatenate strings!? Of all the operators I've been reading about they have never mentioned comma as a concatenater!!!

I guess I got the HTML mandate backwards, I thought it was all capitals....

Thanks for the code.....


Quote:
Originally Posted by Kalle View Post
You are using in_array incorrectly in the second sample code; You are iterating over a block of elements and pass the second parameter to in_array() as a string where it would expect an array, further though the first parameter for in_array() does not work as a wildcard, it will see if theres an element with that content in this case ISBN and nothing else, meaning it will always return false here.

What you need to do is to use something like str[i]str():
PHP Code:
...
if(
stristr($id'ISBN') !== false)
{
    ...
}
... 
Ohhh I was close. I did try strstr but didn't mention it because I thought I was way off....never tried stristr though.....



Quote:
Originally Posted by Salathe View Post
If you want to go the regular expression route, there's another function which is much friendlier to arrays: preg_grep.

Example
PHP Code:
$test  = array('Wfjheighe','ISBN:454656','ISBN:9090943');
$isbns preg_grep('/^ISBN:\d+$/Di'$test);
print_r($isbns); 
Output
Code:
Array
(
    [1] => ISBN:454656
    [2] => ISBN:9090943
)
I'll take a look at your code as well, I need to get better at Regex....

As always thanks!
allworknoplay is offline  
Reply With Quote
Old 05-03-2009, 03:48 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Thanks, I had no idea that you can use commas to concatenate strings!? Of all the operators I've been reading about they have never mentioned comma as a concatenater!!!
That isn't a concatenater, echo can take multiple parameters, that's what the comma is (like you would a function).
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search array benton General 3 03-29-2009 12:31 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
(array) or array()? Orc General 4 01-20-2008 07:52 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 10:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design