Processing a string for next non-whitespace character

Hi,

Ok, I’m trying to search forward in a string for the next non-whitespace character. The old school way is pretty standard but I’m looking for a better or, shall we say, a method more inline with native PHP advantages.

There’s a lot online, and please, don’t think I’ve not searched, but most of the junk I’ve tried to wade through contains too many assumptions. Either the OP wasn’t as completely clear, or as is more common, writer’s tend to assume facts not in evidence.

Here’s my task,

I’ve got some character data held in a big string. Say I’m at position 900 and I’ve moved forward into a block of whitespace that I need to walk past to find the next non-whitespace character. Now I’ve never completely gotten my head wrapped around RegEx expressions and was not really sure this is the way to go since almost EVERY discussion revolves primarily around determining whether or not a match is present, but I recently found the PREG_OFFSET_CAPTURE flag which is precisely what I need.

At its simplest, I’ve tried,

[php]
if (preg_match(’[^\s]’, $configcpp, $m, PREG_OFFSET_CAPTURE, $tpos)) {
EchoLog("…Next Char found at [{$m[0][1]}]");

} else {
  EchoLog('...preg_match FAILED');
}

[/php]

but this always fails. Surely, this is a common task. Especially in language parsing. Hell, I don’t know anymore, with today’s php I’d not be surprised if an entire text file could be exploded into a array of non-whitespace character blocks in one line! :slight_smile:

Thanks for any thoughts.

More on this. I’m not sure preg_match IS the way to go here, I don’t actually need or want to search the entire string, I just want to search from where I currently am, forward until the next.

What are you actually trying to do as a whole?

Are you reading the file in as a single block or in chunks?

I was going to ask the same exact thing.

Sponsor our Newsletter | Privacy Policy | Terms of Service