array search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

array search

Post by yshaf13 »

hi, can someone please help me out with an array search function that would find any number surrounded with a "^" before and after?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I usually don't do this, so consider yourself lucky :) How I would do it

Code: Select all

$stack = array('test string ^3423432^', '^432423^ test string', 'dfdsffdsfd ^342342^');

$found = array_map(create_function('$stack', 'preg_match("#\^([0-9]+)\^#", $stack, $matches); return $matches[1];'), $stack);
A more simple version would be to use a foreach(), and preg_match using the above pattern and place $matches[1] into a new array of numbers.

//outputs: Array ( [0] => 3423432 [1] => 432423 [2] => 342342 )
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

thank alot, i really do feel lucky!
Post Reply