Whew that’s a long title.
Basically, i have a string that I have hashed out – for storage in a database. It looks like this:
Name#123#345#456#678#789#
Now, the problem is – I need to get the first value from this string – before the first ‘#’ – but it could be of completely variable length. This poses a problem – if all sections were of a set length this would be far easier. Not to mention that the string as a whole could have a variable number of segments.
The solution – as I have found – is to use a nasty looking string in PHP to parse it out. For the sake of this example, let’s call our nasty string up there $toParse.
<?php $name = substr($toParse, 0, strpos($toParse, "#")); ?>
This will, in turn, set the variable $name to ‘Name’.
Now – let’s say that I need to actually parse all of these sections. This is also quite easy to accomplish.. We just execute this, plus one more bit – recursively.
<?php
$workingString = $toParse;
//While we still have some string left
while ($workingString != ""){
$name = substr($workingString, 0, strpos($workingString, "#"));
//Remove the previous entry and the previous hash
$workingString = substr($workingString, (strpos($workingString, "#")+1));
//Do whatever you intend to do with your $name result
}
?>
And… That’s pretty much it.
Recent Comments