RSS
 

Posts Tagged ‘regexp’

Advent Of Code 2015: Day 8

09 Dec 2015
CTF: Advent Of Code 2015
Link to challenge: http://adventofcode.com
Date Completed: 09 December 2015

Challenge

Solution

A fairly simple puzzle. My approach was to add up the strings together and use regular expressions to replace special characters with a * character so that the python len function would provide me with the correct result.

Script:

 
2 Comments

Posted in Advent Of Code 2015

 

Advent Of Code 2015: Day 5

05 Dec 2015
CTF: Advent Of Code 2015
Link to challenge: http://adventofcode.com
Date Completed: 05 December 2015

The Challenge

Solution

Another quick and simple Python solution!

 
No Comments

Posted in Advent Of Code 2015

 

WordPress: Regexp and Post Views Counter Plugin

28 Nov 2015

Unfortunately, the WordPress theme I am using did not show any views for each post. While I already had an analytics account, I wanted to display the number of total unique views on my website.

I quickly found a plugin called the Post Views Counter by Dfactory. I installed it and configured it so I could use the shortcode it produced ([post-views]) in my theme.  However, the shortcode was formatted and the output contained some HTML that I simply did not need.

To demonstrate, let me print out the shortcode in this post:

The output from above (if you view source) is:

Note: The 0 will obviously be different (to match the views this post has).

I intend to use PHP regexp to get rid of all of the html and leave only the view count so I can put it into my theme. However, I first need to print out the shortcode within <pre></pre> tags because I suspect there is some whitespace in it.

So I use the follow PHP code to test:

Here we are using the WordPress function  do_shortcode to print out the content.
The results from inspecting the page source are:

Great so we now know what the actual output looks like. Now we can write the simple regexp we are going to use. I recommend using a regexp tester such as this one (http://www.phpliveregex.com/) so you can test the pattern as you go.

I ended up using the following pattern:

I used the s flag so that newlines were also matched.

Finally, I performed a match to get the view count:

 
4 Comments

Posted in Website Development