Currently selected: programming bits (page 1)

customized image tags

Posted on: 14 Oct, 2009
Read more in notebook: programming bits

As you probably know I've moved and redesigned my website several times since it's original inception and one thing I quickly decided is that it's no good putting real image tags right into my blog posts because then if (or let's face it when) I redesign my website I have to troll through all the posts wrapping the images in html tags or changing them into links for a gallery, or removing the links, or whatever new idea seems desirable at the time. Instead I've replaced the html image tag with a tag of my own. Not to surprisingly this too has gone through several versions but I'm quite proud of the current one and I even wrote proper regular expressions rather than the slightly dubious and a bit hacked together parsing solution I had previously. My current tag looks like this: <my_img id=999>

My parsing code is rather simple, I just go through each my_image tag and replace it with a proper html image tag after retrieving the image name from the database based on it's id. I probably could change it to access the database once for all images but since my posts rarely have more than a few images this seems hardly worth the extra complication.

function replaceImgTags($string) {
      $db = new MySQlImageConnector();
      $found = true;
      $regExpr = '/<[ ]*my_img[ ]+id=\"(\d+)\"[ ]*\/>/';
      $matches;
      preg_match_all($regExpr, $string, $matches, PREG_SET_ORDER);
      foreach ($matches as $matchSet) {
            $loc1 = stripos($string, $matchSet[0]);
            $size = strlen($matchSet[0]);
            $image = $db->getImageById((int)$matchSet[1]);
            $tag = "<p><img src=\"".CB_IMAGE.$image->getFileName()."\"";
            $tag .= " alt=\"".$image->getName()."\" /></p>";
            $string = substr($string, 0, $loc1).$tag.substr($string, $loc1+$size);
      }
      return $string;
}

While I was writing this post I discovered that I don't have a good way of formatting code snipits in my blog and I don't really want to have to hand format each one individually so I added a <my_code> tag with a processor to replace the newlines and spaces and wrap it in a paragraph with a css style.

function replaceCodeBlockTags($string){
      $found = true;
      $reg ='/<[ ]*my_code[ ]*>(.*?)<[ ]*\/[ ]*my_code[ ]*>/s';
      $matches;
      preg_match_all($reg, $string, $matches, PREG_SET_ORDER);
      foreach ($matches as $matchSet) {
            $match = $matchSet[1];
            $loc1 = stripos($string, $match);
            $size = strlen($match);

            //keep all the special characters
            $match = htmlentities($match);
            //replace newlines with breaks
            $match = str_replace("\n", "<br />", $match);
            //replace tabs with a bunch of spaces
            $match = str_replace("   ", "&nbsp;&nbsp;&nbsp;", $match);
            $match = "<p class=\"code\">".$match."</p>";
            
            $string = substr($string, 0, $loc1).$match.substr($string, $loc1+$size);
            
      }
      return $string;
}

symfony and twitter

Posted on: 16 May, 2009
Read more in notebook: programming bits

I'm currently working on adding my twitters to cleverbit.org and I'm trying to use the idTwitterClientPlugin. I thought it was worth mentioning that there is a bug in this plugin just in case someone can manage to google and come up with this blog entry they can save themselves some time. On line 92 of idTwitterClient.class.php the name of the username-password string should be 'userpwd' if you want to use curl. It needs to match the name of the constant curl expects in order to include the username-password string in GET requests. I think there may also be an issue with the date encoding of the 'since' field in the UserTimeline function but that was one of the first things I commented out and I haven't gone back to check it yet.

undefined symbols

Posted on: 22 Apr, 2009
Read more in notebook: programming bits

What I learned at work this week:

If a method is undefined and the error shows just the method name this is a standard c++ linking error. If the entire method signature is shown this is a good sign that you forgot the extern "C" bit.

Now if only it hadn't taken me half a day to figure that out... Well that and the conversion of strings from C to FORTRAN but lets just not even go there.

symfony tips

Posted on: 15 Mar, 2009
Read more in notebook: programming bits

Here are some tips I've gathered while learning to use symfony:

  1. Spend time to properly describe your database tables in the schema.yml file especially foreign keys and join tables, it will make your life much easier when you get to the backend part of the application
  2. If you get a routing error check the auto generated links in the templates - they may be trying to create a link to something you've disabled (like edit links in the front end off the app)
  3. Name tables in the singular or the variable names will be a bit funny
  4. Name the id field in the table "id"