Connecting Tech Pros Worldwide Help | Site Map

sure shorter way to do this?

Jonathan Moss
Guest
 
Posts: n/a
#1: Jul 17 '05
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


jn
Guest
 
Posts: n/a
#2: Jul 17 '05

re: sure shorter way to do this?


"Jonathan Moss" <jon@a.com> wrote in message
news:bv8is6$i2e$1@sparta.btinternet.com...[color=blue]
> I am currently working on a StructuredText interpreter for PHP. The[/color]
current[color=blue]
> task in inline parseing of markup. for example looking in a plain text[/color]
block[color=blue]
> 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[/color]
to[color=blue]
> 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
>
>[/color]


You should try regular expressions instead...


Closed Thread