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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
"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... |
|
|||
|
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 |