Revision [4577]

This is an old revision of ProgrammingHelp made by GmBowen on 2005-01-13 01:48:09.

 

Programming Help


 


Many people work at developing various aspects of code for wikka and yet we often struggle for hours, or even days, to solve programming problems that others in the wikka community know the answer to. Even more of an issue, sometimes coding problems aren't rectified until after initial code releases and feedback from other members lead to new code being posted (which means that the old code might now be running on others' wikka implementations) because the initial developer could not figure out how to solve a problem. To reduce these problems, this page is intended for people to be able to pose programming issues they've encountered and have the opportunity to draw on the knowledge of others' in our community as to how to resolve those issues. There are two sections...."Issues Needing assistance" & "Solved Issues". When a user has either solved an issue on their own that they've posted, or received help on the issue that satisfactorily addresses it, then the solution should be posted and it should be moved from the first to the second category.

Issues Needing Assistance



Resolved Issues


Replace
How would you replace every 2nd occurence of a "needle" in a haystack string? I'm sure you can see where I'm going with this, I want to parse output from a table read so that **'s and //'s are replaced alternately with and etc in a string so I can minimally format table content in the output tales in the forum and calendar actions after stripping out html on the way "in" to the database (both of which pick up on what other wikkausers suggested as desireable features for them). I've searched everywhere on this & spent ages on it and have found bupkuss. Thanks for any guidance. -- GmBowen
Finding a function that replaces only the first occurence in the string led me to the solution. I've coded 3 functions that can be included to turn bold & italics wikka code into html tags for output into a table (such as in my scheduler & wikka forum). Code below....
function str_replace_once($needle, $replace, $haystack) {
   // Looks for the first occurence of $needle in $haystack
   // and replaces it with $replace.
   $pos = strpos($haystack, $needle);
   if ($pos === false) {
       // Nothing found
       return $haystack;
   }
   return substr_replace($haystack, $replace, $pos, strlen($needle));
}

function bold_formatting_replace($haystack) {
    //replaces bold wikka formatting (**) with html bold tags
    $needle="**";
    $count=(int)(substr_count($haystack, $needle)/2);
    $replace="<strong>";
    $replace2="</strong>";
    while ($counter<$count)
    {
        $counter++;
        $haystack=str_replace_once($needle, $replace2, str_replace_once($needle, $replace, $haystack));
    }
    return $haystack;
}

function italics_formatting_replace($haystack) {
    //replaces italics wikka formatting (//) with html italics tags
    $needle="//";
    $count=(int)(substr_count($haystack, $needle)/2);
    $replace="<em>";
    $replace2="</em>";
    while ($counter<$count)
    {
        $counter++;
        $haystack=str_replace_once($needle, $replace2, str_replace_once($needle, $replace, $haystack));
    }
    return $haystack;
}

                $printtext = (stripslashes($row['for_text']));
        $brackets = Array ('/{{/i', '/}}/i');
        $changeto = '|.|';
        $printtext = preg_replace($brackets, $changeto, $printtext);
        $remove = array("#%", "======", "=====", "====", "===", "==", "[[", "]]", "||", "
", "
", "
", "
 
");
$printtext = str_replace($remove, , $printtext); $printtext = $wakka->format($printtext); %% But, it doesn't actually strip out the
or
tags, and I've found no way of removing double quotes. Any ideas? -- mike ==Multiple copies of actions== Not being able to put multiple copies of an action on a page. Can be caused by multiple copies of the same functions being loaded. Solution: have the functions "included" from a separate file using include_once() which stops repeated loading of the functions. Example where implemented: WikkaForum ==Only single copies of generated images on a wikka page== In developing my WikkaGrapher action I have it both generating an image and wikka code (that calls an action) that can be placed on a page to place the graph on the page. The action is below (saved as graph.php) and examples of the code are at http://gmbowen.educ.unb.ca/wiki/wakka.php?wakka=Sandbox5 (the code generating interface is at [[http://gmbowen.educ.unb.ca/wiki/wakka.php?wakka=Sandbox6 SandBox6]]). Only the code for the first image is processed for some reason (as can be seen at [[http://gmbowen.educ.unb.ca/wiki/wakka.php?wakka=Sandbox5 Sandbox5]]). Not surprisingly, I'd like users to be able to place more than one graph on a page. Ideas? The graphing engine is at http://www.phpclasses.org/browse/package/1993.html. %%(php)
'; ?>%% ~& Three problems with that code: ~~- the break tag should be written as
(note the forward slash) ~~- just like the break tag the img tag should be closed - but isn't: - you need ' />' at the end (not just '>'); ~~- once you do that the main problem causing only one image to appear becomes more visible: the src attribute has only an opening " but not a closing one ~& Change the code as follows: %%(php)
'; ?>%% BTW, why not use the nice features of [[http://www.phpclasses.org/browse/package/1993.html class.graphic.php]] to let the user specify the data it needs as separate action parameters and let the class generate the query string? That would be much more user-friendly. That's what OO is all (well, mostly) about: hiding the complexities... (The form is horribly messy though...)--JavaWoman ~~&you should *blush* again....look at [[http://gmbowen.educ.unb.ca/wiki/wakka.php?wakka=Sandbox6 SandBox6]] and you'll see I am essentially using his interface....I spent 2 days adapting it so it'd work in wikka & give a preview of the graph. A "save" feature is next (Carlos sent me a copy of his code with that incorporated). And perhaps a series of "if" statements so parameters that are blank are not added onto the string -- GmBowen ~~~&Yes, I commented on that messy form, didn't I? (I even updated the link to it. ;-)) What I meant is that your **action** (apparently) isn't using the class interface to generate the query. Which makes the action hard to use. You could still have it accept the $code paramater, but at the same time have it accept the actual parameters the class can use to generate the query, so it's easier to directly create and/or update the action as embedded in a page. --JavaWoman ~~~&Well, I couldn't use his interface directly in wikka because it was non-compatible. But I'm generating his code in almost exactly the same way he did (but w/o java scripts), I'm just using post instead of get. In addition, and I'm sure you'll :) at this, I removed the code that was non-compliant (I had a computer science major looking over my shoulder at one point when I was working on this who pointed out what the compliance problem was). It's quite possible that I'm missing your point here, because I do think I'm doing what you're saying I'm not. Hmmmm.... --GmBowen ~~~~&How is it not compatible? A class is about the easiest to integrate, I would think. Anyway, show us your action code then so we can see what you are //actually// doing... --JavaWoman ~~~~~&Now at GraphMaker. His class had embedded formatting all over the place that threw-off wikka....I'm not sure it's a "true" class in the sense of what one of those is (easily embeddable)....it's more like a single-script standalone program.--GmBowen ~~~~~~&Mike, it's definitely a valid class, but the html() method is producing a whole **page** rather than just body content, so you'd need to subclass the class and override the html() method at least. That makes it somewhat harder to use in an application like Wikka, or anything that needs body content only instead of a whole page, but it doesn't mean you can't use it. (The html() method should be refactored though, so it uses a body() method to only generate that part.) Like I said, if you want to use classes, learn how to use classes - sorry. It's the way you are using the file in this context that is causing the problems; it comes with a bunch of examples how to use it - have you tried any of those before even trying to integrate with Wikka? I'd advise you to do that sso you get a feel for how to interface with the class in general, and what it's capable of. --JavaWoman ~~&I could tell it was something with the closing tags (by looking at properties there were things at the end of the code that shouldn't be) but I couldn't figure out what. I tried multiple things, and obviously one resulted in the loss of the end '>'. Why does it have to be '/>' and not just '>'?? (there are vagaries to php that vary from html that I'm not clear on). Thanks JW. Works like a charm now. ~~~& Because the html-syntax is (opening tag - closing tag). There was (and is) no or , and it wasn't necessary in older html-standards, so they defined the form for this cases, which opens and closes the tag. --NilsLindenberg ~~~&Hmmm, okay. I'll have to work at remembering that...ugh. Thanks Nils. (Now off to bed). -- GmBowen ~~~&Actually there //is// a closing tag for every opening tag in XHTML, so exists, and so does . And in XHTML **every** tag **needs** to be closed - which implies that every tag also has a closing tag. However when a tag is //empty//, you can "shortcut-close" it by ending the tag with '' instead of closing it with ''. That's less to type - but an additional advantage is that some browsers (still) don't support the "full" form but do support the "shortcut" form - especially if you use "compatibilty mode" by putting a space before the slash: . The latter form is what we use in Wikka , to ensure compatibilty with many browsers. --JavaWoman ~& Another note: it's probably wise not to have the action accept the $code value blindly; at the very least first build the URL and then URL-encode it to prevent any attacks. Without that, the code is a security risk. (Isn't it surprising how many problems there can be with a single short line of code? **;-)**) --JavaWoman ~~&I need more guidance on this I'm afraid (my non-programming background leaping to the fore ;-) )....do you mean do a urlencode($code) in the ##graph.php## file before the "$url="./scripts/graphmaker.php?".$code; ~~&$url=urlencode($url); ~~&and then echoed it, and it didn't work. So I played around some and found that even if I entered http://gmbowen.educ.unb.ca/wiki%2Fscripts%2Fgraphmaker.php (with the / replaced with % 2 F) then it didn't work. No surprise then that the urlencoded output didn't work. So, I encoded and then decoded it and that seemed to solve the problem (and, I'm assuming, the security issue). Is this acceptable? ~~~&Exactly what do you mean by "didn't work"? What happens when you use a URL-encoded URL for the img src attribute? --JavaWoman ~~&I mean I got a little box with a red x on the wiki page. I played with it lots in the ##graph.php## file and only got the same. So I echoed the code out and it looked fine (both before and after the urlencode was done) so I placed the imgsrc reference in the URL bar....still nothing...didn't generate a graph (or anything). So I took only the http://gmbowen.educ.unb.ca/wiki%2Fscripts%2Fgraphmaker.php"" bit and still nothing was generated....it should have given me the graph form at least. So, I tried replacing more and more of the %2F's with a slash (in the URL bar) and when they were all replaced THEN it generated the graph form (which my class has been changed to do by default). So I went to php.net and looked to see if there was a way to urlunencode & found there was. So I changed the line (urlencode($code)) (which was generating encoded img src reference to (urldecode(urlencode($code))) and it worked just fine. I subsequently changed it to (urldecode(urlencode(strip_tags($code)))) so that it wasn't possible to put tags into what was being passed at all and that generated graphs too. Because of both striptags and the urlencoding I think I've addressed your concern because urldecoding shouldn't affect the effect of urlencoding should id?

Unresolvable Issues


Double-Click Edit
Is there any way to insert code into an action that disables the double-click editing on a page? I thought I saw something on this site but cannot find it anymore.
Rats. I was hoping for a workaround. - m


CategoryDevelopment
There are 5 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki