473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1671
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
12
by: Secret Guy | last post by:
I'm trying to understand generally how to make a grid of variously sized boxes using CSS. The first idea I had was to make a box where I can see the margin, padding, border and content edges, and...
1
by: Susan Addams | last post by:
I know there's a way to do this -- I've seen it before but can't remember how it was done. When using CSS positioning, how do you make sure the copyright notice ends up at the bottom of the...
1
by: PHP2 | last post by:
I have one large number with 18 decimal places for license key. have any way that I transfer my number into shorter number or string with some method?
8
by: Servé Lau | last post by:
Sometimes I code like this: char *p = strchr(buf,'\n'); if (p) *p = 0; But I've also seen the shorter expression: buf = 0;
4
by: Dean Slindee | last post by:
Is there a shorter way of referring to another forms controls/events than creating a class hold forms, adding a form item each time a new form is loaded, and then looping thru the class of forms to...
8
by: **Developer** | last post by:
I'd like to make sure a string is no longer that some value. What is the best way to do that. For example, it would be nice if I could do: ControlComboBoxName.Text = name.Substring(0,...
1
by: Daikide | last post by:
I need average for my program. And I'm using it multiple time in my program. I'm doing this in PVSS: So say average float is 2.0013005 (FOR EXAMPLE) but the number in the end can be higher......
3
by: skanemupp | last post by:
is there anyway to make this shorter? i hate having these big blocks of similar-looking code, very unaesthetic. maybe doesnt matter good-code-wise? anyway can i make some function that makes this...
4
by: raylopez99 | last post by:
Please consider the below and how to make name references shorter-- there has to be a way. RL using System; namespace MyNamesSpace1 { class ManagerClass
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.