Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression 4

Status
Not open for further replies.

disord3r

Programmer
Apr 12, 2002
189
US
I'm sure this has been asked before (actually, I think I've SEEN it here before), but my keyword searching appears to be a bit off today.

I'm looking for a regular expression that will match data between certain characters (a pair of # and a pair of %, to be exact). I have this file being fed to me, and there's a lot of junk in there that I don't need to deal with. Randomly throughout this document, there is %something% and #something_else#, and that's what I'm interested in.

I'm not too good with regular expressions, but what I was hoping to do was parse through the feed (twice, if necessary) and end up with an array of % matches, and an array of # matches (there may be multiple matches of each in this file).

I'm off to look get help elsewhere on the web, but if anyone is quicker at typing than I am at reading, the help would be greatly appreciated.


Thanks!
 
Here's an example which illustrates...
Code:
$string = "la #de# a duh";
$pattern = "/#(.*)#/";
preg_match($pattern, $string, $matches);

print($matches[1]);


for more reading... including how to properly handle things like
la #de# da #doo!#
 
That worked somewhat, but $matches[2] (or any index greater than 1) is empty. It's driving me nuts.

There are 4 instances of #something# in the feed file, so $matches should have 5 elements, correct?
 
Let me clarify my previous post a little bit.

$matches[0] contains everything I would expect it to, but count($matches) returns 1. This throws off a loop that I'm using.

Any idea why it comes back with a 1? According to PHP docs, that means that count() thinks it's not an array.

DRJ - I'll try your suggestion.


Thanks for the help folks!
 
pre_reg_match_all has me in the right direction.

Thanks all!
 
Make that preg_match_all (it's been a LONG day).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top