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!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi all. I coming from a Java background and I'm having a little trouble with parsing.
I have an old data file that contains non-ASCII characters. I need to parse these out and replace them with entities.
I also need to keep track of whether I am within a backslash or not. I figure I can do this with a state variable that is either on or off.
My main problem is figuring out how to go through the file character by character. I think I need to do it this way so that I can keep track of the backslashes.
Here is what I have so far:
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
<?PHP
$string = "this is the contents of the file blah blah blah...";
for($i = 0;$i < strlen($string);++$i)
{
$curr_char = $string{$i};
//do what you need to do here
}
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Yeah, I was just using substr(), and it didn't seem to be the best way to go about it. I didn't realize I could treat it as an array like that. Thanks for the help.
<?PHP
$string = "this is the contents of the file blah blah blah...";
for($i = 0;$i < strlen($string);++$i)
{
$curr_char = $string{$i};
//do what you need to do here
}
?>
That is one of the coolest things I've seen. I need to try that one of these days.
eysikal wrote:Yes, that's part of it. We're trying to convert some old plain text data over and it has some non-ASCII characters that need to be replaced.
I'd go with your idea of using ord() byte-for-byte and remove anything that doesn't work
Everah wrote:That is one of the coolest things I've seen. I need to try that one of these days.
Don't forget that PHP is written in C. Most of the thing you do just translate back to C stuff. In C, strings are just arrays of characters " char foo[50];" so this feels fairly natural anyway
d11wtq wrote:Don't forget that PHP is written in C. Most of the thing you do just translate back to C stuff. In C, strings are just arrays of characters " char foo[50];" so this feels fairly natural anyway
Last time I did anything in C was in 1992 computer science in college. I don't remember a thing from that time at all. No, not even one little bit. I do recall, however, wishing that everything was as easy to pick up on as PHP. Maybe if Rasmus' little project was widely in use across universities I may have been better at it.