Preg_match returning numbers?

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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Preg_match returning numbers?

Post by Citizen »

Code: Select all

$searchType = "title";
$tempkey = "sgame.title.foobar";
                    
$pattern = '/sgame' . $searchType . '.(.*?)/';
preg_match($pattern, $tempkey, $matches, PREG_OFFSET_CAPTURE);
 
print_r($matches);
Prints this:

Code: Select all

Array ( [0] => Array ( [0] => title. [1] => 6 ) [1] => Array ( [0] => [1] => 12 ) )
I'm looking to put 'foobar' into a variable that I can then use, but all that comes up are these numbers. Any ideas?
scriptah
Forum Commoner
Posts: 27
Joined: Sat Mar 15, 2008 8:58 pm
Location: Long Island, NY

Re: Preg_match returning numbers?

Post by scriptah »

Code: Select all

 
$pattern = '/sgame\.' . $searchType . '\.?(.*)?/';
 
I think you have a problem with your pattern ...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Preg_match returning numbers?

Post by John Cartwright »

RTM :wink:
preg_match(..., PREG_OFFSET_CAPTURE); wrote:If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at index 0 and its string offset into subject at index 1.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: Preg_match returning numbers?

Post by Citizen »

I read the manual, but I didn't quite understand the implication of the offset_capture, which is why i came here :)

And thanks to both of you, the correction of the pattern and removal of offset did the trick.
-Cit
Post Reply