gregIML Posted January 30, 2008 Share Posted January 30, 2008 Hi, I'm trying to tokenize a text file or maybe an xml file depending on what is possible. Ideally i'll have an xml with <date>the date</date> <title>the title</title> <content>the content</content> Is it possible to tokenize on the end tags of each field. i.e </*> so far i can only find ways to tokenize on individual characters using the strtok() function, not a string of characters. any help would be greatly appreciated. thanks in advance Greg IML Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/ Share on other sites More sharing options...
effigy Posted January 30, 2008 Share Posted January 30, 2008 Depending on what you're doing, an XML parser may be better. Something like this? <pre> <?php $data = <<<DATA <date>the date</date> <title>the title</title> <content>the content</content> DATA; $pieces = preg_split('%</[^>]+>%', $data, -1, PREG_SPLIT_NO_EMPTY); foreach ($pieces as &$piece) { $piece = htmlspecialchars($piece); } print_r($pieces); ?> <pre> Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-453433 Share on other sites More sharing options...
gregIML Posted January 30, 2008 Author Share Posted January 30, 2008 That's really useful thank you. I'm now getting the situation where it is printing the first tag (slightly overlooked that one) i need what's between the tags, how is this done? Thanks in advance Greg IML Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-453517 Share on other sites More sharing options...
effigy Posted January 30, 2008 Share Posted January 30, 2008 <pre> <?php $data = <<<DATA <date>the date</date> <title>the title</title> <content>the content</content> DATA; preg_match_all('%<([^>]+)>(.*?)</\1>%', $data, $matches); print_r($matches); ?> <pre> Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-453560 Share on other sites More sharing options...
gregIML Posted January 30, 2008 Author Share Posted January 30, 2008 thank you, that's really helped Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-453576 Share on other sites More sharing options...
gregIML Posted January 31, 2008 Author Share Posted January 31, 2008 I'm still really struggling with this. i have an xml file <Date>30th January 2008</Date> <Title>Hello</Title> <Content>some content goes here</Content> I want to output 30th January 2008 Hello some content goes here how can i do that? so far this <pre> <?php $data = <<<DATA <date>the date</date> <title>the title</title> <content>the content</content> DATA; preg_match_all('%<([^>]+)>(.*?)</\1>%', $data, $matches); print_r($matches); ?> <pre> this outputs this Array ( [0] => Array ( [0] => 30th January 2008 [1] => [2] => some content goes here ) [1] => Array ( [0] => Date [1] => Title [2] => Content ) [2] => Array ( [0] => 30th January 2008 [1] => Hello [2] => some content goes here ) ) how do i just get the content and print it out. If someone could just even direct me to the right php function i'll be able to do it, i really cant find this. thanks Greg IML Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-454422 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 instead of print_r use something like $xmlvals=array_combine($matches[1],$matches[2]); now u have a spanking new array with $key => $val equivalents echo "Title: $xmlvars[title]<BR>"; echo "Date: $xmlvars[date]<BR>"; enjoy Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-454427 Share on other sites More sharing options...
gregIML Posted January 31, 2008 Author Share Posted January 31, 2008 Thank you laffin, that is great help. I was using that xml as an example, let's say i didnt know what the xml tags were, i just want to print out what is between each tag in order, so i need to use the position in the new array not the name, so i want to do something along the lines of $xmlvars[0]; not $xmlvars[title]; how would i do that? Greg Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-454466 Share on other sites More sharing options...
gregIML Posted January 31, 2008 Author Share Posted January 31, 2008 OK unless you do have a solution for my question please dont worry. I've realised that if the tags in the xml were numbers i can scroll through them no problem using a for loop. Thanks for both of your help Greg IML Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-454504 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 u can keep them as they are and using foreach to go thru the array foreach($xmlvars as $key => $val) echo "$key: $val<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/#findComment-454505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.