Bluehost.com Web Hosting $6.95

sure shorter way to do this?

This is a discussion on sure shorter way to do this? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I am currently working on a StructuredText interpreter for PHP. The current task in inline parseing of markup. for example ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-28-2004
Jonathan Moss
 
Posts: n/a
Default 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


Reply With Quote
  #2 (permalink)  
Old 01-28-2004
jn
 
Posts: n/a
Default Re: sure shorter way to do this?

"Jonathan Moss" <jon@a.com> wrote in message
news:bv8is6$i2e$1@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...


Reply With Quote
  #3 (permalink)  
Old 01-28-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: sure shorter way to do this?

On 2004-01-28, jn <usenet*spamistehsuckremovetoreply*@jasonnorris.ne t> wrote:
> "Jonathan Moss" <jon@a.com> wrote in message
> news:bv8is6$i2e$1@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...


Could you provide a regular expression then? And does it still work when
tags are nested? I don't think so.

When it comes down to parsing text, it is nice to know about the State
pattern. Oh, and there is also a PEAR phpBB-code parser.

--
http://home.mysth.be/~timvw
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:18 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0