470,874 Members | 1,769 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,874 developers. It's quick & easy.

sure shorter way to do this?

I am currently working on a StructuredText interpreter for PHP. The current
task in inline parseing of markup. for example looking in a plain text block
and finding the sections that should be italic. The italic sections are
marked either side with a '_' character. e.g

'This paragraph should have some _italic text in it_'

The is split up into tokens

[0] = "This paragrapgh should have some ", type['NONE']
[1] = "", type['BEGIN_ITALIC']
[2] = "italic text in it", type['NONE']
[3] = "", type['END_ITALIC']

The above token stream is needed because the document generated can be
outputted into a number of markups html, pdf etc.

the function bellow is what I have come up with but it isn't very elegant to
say the least. I would really appreciate you input.

thanks,
Jon
function parseItalic() {
foreach ($this -> tokens -> tokens as $token) {
if ($token -> type == $this -> ptp -> types['NONE']) {
$state = false;
$text = $token -> text;
$string = "";
$tokens = null;
if (strpos($text,"_")){
for ($i = 0; $i < strlen($text); $i ++) {
if ($text {$i } == '_') {
//terminate current string and add to $tokens
$tokens[] = new PTPToken($string, $this -> ptp ->
types['NONE']);
$string = "";
//toggle state and add appropriate token
if ($state) {
$state = false;
$tokens[] = new PTPToken("", $this -> ptp ->
types['END_ITALIC']);
} else {
$state = true;
$tokens[] = new PTPToken("", $this -> ptp ->
types['BEGIN_ITALIC']);
}
} else {
$string .= $text {$i};
}
}
if ($string != ""){
$tokens[] = new PTPToken($string,
$this->ptp->types['NONE']);
}
$this -> tokens -> insertTokens($token, $tokens);
$this -> tokens -> removeToken($token);
}
}
}
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.569 / Virus Database: 360 - Release Date: 26/01/2004
Jul 17 '05 #1
1 1591
jn
"Jonathan Moss" <jo*@a.com> wrote in message
news:bv**********@sparta.btinternet.com...
I am currently working on a StructuredText interpreter for PHP. The current task in inline parseing of markup. for example looking in a plain text block and finding the sections that should be italic. The italic sections are
marked either side with a '_' character. e.g

'This paragraph should have some _italic text in it_'

The is split up into tokens

[0] = "This paragrapgh should have some ", type['NONE']
[1] = "", type['BEGIN_ITALIC']
[2] = "italic text in it", type['NONE']
[3] = "", type['END_ITALIC']

The above token stream is needed because the document generated can be
outputted into a number of markups html, pdf etc.

the function bellow is what I have come up with but it isn't very elegant to say the least. I would really appreciate you input.

thanks,
Jon
function parseItalic() {
foreach ($this -> tokens -> tokens as $token) {
if ($token -> type == $this -> ptp -> types['NONE']) {
$state = false;
$text = $token -> text;
$string = "";
$tokens = null;
if (strpos($text,"_")){
for ($i = 0; $i < strlen($text); $i ++) {
if ($text {$i } == '_') {
//terminate current string and add to $tokens
$tokens[] = new PTPToken($string, $this -> ptp ->
types['NONE']);
$string = "";
//toggle state and add appropriate token
if ($state) {
$state = false;
$tokens[] = new PTPToken("", $this -> ptp ->
types['END_ITALIC']);
} else {
$state = true;
$tokens[] = new PTPToken("", $this -> ptp ->
types['BEGIN_ITALIC']);
}
} else {
$string .= $text {$i};
}
}
if ($string != ""){
$tokens[] = new PTPToken($string,
$this->ptp->types['NONE']);
}
$this -> tokens -> insertTokens($token, $tokens);
$this -> tokens -> removeToken($token);
}
}
}
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.569 / Virus Database: 360 - Release Date: 26/01/2004

You should try regular expressions instead...
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Krakatioison | last post: by
12 posts views Thread by Secret Guy | last post: by
1 post views Thread by Susan Addams | last post: by
1 post views Thread by PHP2 | last post: by
8 posts views Thread by Servé Lau | last post: by
3 posts views Thread by skanemupp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.