Advertise here


Tag cloud


Posts Tagged ‘useful script’

Regex link removal script

Posted on Thursday, February 16th, 2012 in Web Development

Need to remove links from your content? This regex patten should do the trick.

First you will need to choose a programming language has regex functionality and returns the properties start position, the length and group values of a string.

Then use a substring method to extract the string that matched the pattern.

Use the replace function to remove the link and use group two from the results of the regex function to put back the link text.

(<a\s*href="http://www\.sitename\.com/[a-zA-Z0-9-\?&;%=/_""\s\.]*">)([a-zA-Z0-9-\?&;%=/_"\s\.]*)(</a>)

Example below is using c#.

if (myMatchs.IsMatch(newContent))
{
      Status = "[Match]";

      try
      {
            int totalMatches = myMatchs.Matches(newContent).Count;
            Match keyMatch;
            string m;

            for (int i = 0; i < totalMatches; i++)
            {
                   keyMatch = myMatchs.Match(newContent);
                   m = newContent.Substring(keyMatch.Index, keyMatch.Length);
                   changes += " , " + m;
                   newContent = newContent.Replace(m, keyMatch.Groups[2].Value);
             }
        }
        catch (Exception ex)
        {
              Status = string.Format("[Fail] - {0}", ex.Message);
        }
}
else
{
        Status = "[No Match]";
}

A good idea would be to output the changes to a text file so you can track the modifications.

If you are replacing links in MySql database, using innodb engine use transactions so you can check the changes before committing them.


CSS create tool tip

Posted on Wednesday, August 31st, 2011 in CSS

The content attribute in CSS is pretty useful. It can only be used with the Pseudo properties before and after which essentially determine where the content is going to be outputted.

So a basic example using the content attribute would be to label small items: 09839 048882

.phonenumber:before{ content: 'Phone number: ';}

Essentially this snippet will add “Phone number” in every element that uses this class at the beginning of the text. Pretty useful when there is a mass change of labels needed to be done.

How to make a tool tip just using CSS? Read more


Make navigation elements hover over flash.

Posted on Monday, August 1st, 2011 in Top Tips

I was working on a drop down menu which unfortunately was dropping behind a flash advert, so most of the navigation items were not visible.

There are essentially two things you need to do to combat this small issue. Firstly the flash object (embed tag) needs to have wmode attribute and set to transparent. A param tag also needs to be added, setting the wmode, as seen in the example below.

<embed wmode="transparent" />
<param name="wmode" value="transparent" />

The menu class then needs the z-index set to a very high value in this case: 9 999 999 to layer above the flash item. The item also needs to have an absolute position for the z-index to take affect. The reason for using z-index is because this controls the layers level in the web page, essentially the depth. Flash objects and web forms naturally have a high z-index and this is why most elements appear below them. The simple script can be used over web forms as well, you don’t need to do anything special to the forms.

Z-index the higher the value the near to the top it will be.

.menuItems{
      position:absolute;
      z-index: 9999999;
}

Execute script after images have loaded in

Posted on Thursday, May 19th, 2011 in JavaScript

I found myself needing to run a script to make minor adjustments to the page based on the size of an image, to keep things tidy. The problem I was having was using JQuery’s $(document).ready the script was executing as soon as the DOM was ready which is too early. The other option is to use $(window).load which runs only when the whole page has loaded, guaranteeing that I will get the image width and I can make my adjustments. The script below is used to make the image caption the same width as the image.

$(window).load(function () {
    imgWidth = $("#mainImg").attr("width");
    var caption = $(".imgCaption");
    if (imgWidth < 300) {
        captionWidth = imgWidth - (parseInt(caption.css("padding-left")) + parseInt(caption.css("padding-right")));
        caption.css("width", captionWidth+"px");
    }
});

Concatenate rows into one field

Posted on Wednesday, April 13th, 2011 in MySQL

This article explains how to concatenate rows using SQL into one field.

This example is based on a website that uses tags to relate articles and is using a MySQL database. An article will have multiple tags and each tag is in a row in the tags table. By grouping an article by the news_id field this will return more that one row if the article has many tags. The issue now is you only want to return a single row as the article with the tags nicely grouped together in one field. This can be achieved by using Group_Concat() method which requires the field name to be concatenated. By default each row is separated by a comma but this can be changed by entering a SEPARATOR and then the character straight afterwards.

SELECT story.*, GROUP_CONCAT(tags.tag) as tagGroup FROM story
LEFT JOIN tags ON tags.news_id = story.news_id
WHERE story.news_id = 13
GROUP BY story.news_id

The example above shows one way to use Group_Concat()

GROUP_CONCAT(tbl_list SEPARATOR '-')

This shows how to change the default separator.

This method can be used in other ways to concatenate items, to read further see Group_Concat() dev.mysql.com.


Count rows while using limit

Posted on Sunday, February 27th, 2011 in MySQL

A very useful feature that MySQL provides is the ability to calculate the number of rows returned when using limit.

By using the SELECT parameter SQL_CALC_FOUND_ROWS in your first query and then in the next query use SELECT FOUND_ROWS(); function which will return the number of rows.

SELECT SQL_CALC_FOUND_ROWS col_a, col_b
FROM tbl_name
WHERE col_c = 8 LIMIT 10;
SELECT FOUND_ROWS();

Read more


Insert else update

Posted on Sunday, February 20th, 2011 in MySQL

Here is something that I recently found very useful. If you have a table in your MySQL database which you want to insert a record if it does not exist but update the row if it does. MySQL 5.x has a very neat piece of script to handle this with out any conditional statements.
Read more


Console C# getting database connection string from app.config file

Posted on Monday, November 1st, 2010 in Top Tips

When writing a console application using C# / .NET and you want to connect to a database, the connection string is typically stored in the app.config file. Below is an example of a web.config which is the same an app.config file used in C#.Net. It is essentially an XML file that stores required data to make your application work. It keeps these bits of data in one place, making it easy to manage when things change. There will be no need to hunt down connection strings or other configuration data with in your application code.

To extend further on this top tip I have written a tutorial on how to use CSharp with MySQL.

 <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <ConnectionString>
         <add key="MySQL.DB" value="server=localhost;database=mysqldbname;user=username;password=12345678;" />
       </ConnectionString>

       <system.web>
        <compilation defaultLanguage="c#" debug="true" />
      </system.web>

    </configuration>

To access the connection string the first step would be to make sure that a reference is added to System.Configuration. Then make sure you include it in the class using System.Configuration and the code below is used to access the connection string.To access the connection string the first step would be to make sure that a reference is added to System.Configuration. Then make sure you include it in the class using System.Configuration and the code below is used to access the connection string.

using System.Configuration;

string MysqlConnectionString = ConfigurationManager.ConnectionStrings["dbconnectionstring"].ConnectionString;

This method can also be used to access the app.config file for any web applications.



Web Design Essex | Richard Kotze – Web Technology, Design and Development powered by WordPress | Entries (RSS)