Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/88568-tokenizing-on-a-string-not-a-character/
Share on other sites

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>

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.