Archiv der Kategorie ‘php‘

 
 

PHP File Loads Itself

This is weird idea I had right before I went to sleep last night. I might actually use it for something… have a look:

loadme.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
<?php
  $file = file_get_contents("loadme.php");
 
  $file = preg_split("/\?\>\n/", $file);
  $file = preg_replace("/^/m", "z",$file[1]);
 
  echo $file;
 
  die();
?>
one
two
three
four
 
'; ?>

The above just adds a z in front of each new line. So it outputs:

zone ztwo zthree zfour

It could be used for preprocessing a document. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
<?php require_once("processor.php"); ?>;
 
html
  head
    meta charset="utf-8"
    title (test site)
 
  body
     #nav
       .btn(one) .btn(two) .btn(three)
 
     #left (left side)
 
     #center
        .entry
          h1(Hello This is an entry)
          (this is some text)
        .entry
          h1(Another Entry)
          (more text)
 
     #right (right side)
 
     #footer (Vorbin Saucer 2012)

The php would then turn the above markup into regular html. Kind of interesting…

MediaWiki Api

Here is an example of how to start using the MediaWiki Api. For demo purposes I’m getting data from Wikipedia:

First we need some php for the http request. You need to send a user agent value that contains contact information, otherwise MediaWiki will not let you access any data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
 
  $defaults = array(
    "term" => "water", 
    "limit" => "10"  
  );
 
  foreach($defaults as $key => $value){
    if (isset($_REQUEST[$key])){
     $defaults[$key] =  urlencode($_REQUEST[$key]);
    }
  }
 
  ini_set("user_agent", "Demo for http://zreference.com/");
 
  echo file_get_contents("http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={$defaults['term']}&srlimit={$defaults['limit']}&format=xml");

Then we can use a little jQuery to load and display the data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MediaWiki Api</title>
    <script src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
    <script>
 
      $(function(){
 
        var body = $("body");
 
        var vars = {
          term : "strange attractor",
          limit : 10,
        }
 
        $.post("wiki.php", vars, function(data){
          var parsed = $.parseXML(data);
          var results = $(parsed);
 
          results.find("p").each(function(){
            var curr = $(this);
            var title = curr.attr("title");
            var link = "http://en.wikipedia.com/wiki/" + title;
            var content = "<h1>" + title +"<\/h1>";
            content += curr.attr("snippet");
            $("<div/>", {
              html : content,
              className : "result"
            }).appendTo(body)
              .wrap("<a href='"+link+"' target='_blank'/>");
          });
 
        });
 
      });
    </script>
    <style>
      a{
        text-decoration : none;
        color : black; 
        -webkit-transition : color 0.5s;
      }
      a:hover{
        color : red; 
      }
      .result{
        position : relative;
        font-family : sans-serif; 
        margin-bottom : 10px;
      }
      .result h1{
        font-size : 20px; 
      }
    </style>
    <body>
    </body>
  </head>
</html>

Rather than using jQuery.animate() I decided to use a css webkit transition for the rollover – just for demo purposes.

There’s lots of documentation for this api – there is a nice summary if you visit api.php:

http://en.wikipedia.org/w/api.php

The final result will look like this.

Recent Snippets Part 1

Here are a few snippets that I’ve used recently. They are javascript, mysql, php and some apache directives:

Select rows from a table in random order:

SELECT * FROM `table` ORDER BY `field` RAND();

Fix broken images on a page, good for use with ajax:

1
2
3
4
5
function fixBrokenImages(someDiv){
   someDiv.find("img").bind("error", function(){
         $(this).attr("src", "no-image.jpg");
   });
}

I use this snippet locally to prevent caching in mamp:

1
2
3
4
5
6
7
8
9
<FilesMatch "\.(html|htm|js|css|jpg|png|gif)$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</IfModule>
</FilesMatch>

Working on something with jsfiddle-esque mod rewrite happening, this basically takes up to 3 get vars, but the urls can look like this: mysite.com/one/two/three

1
2
3
4
5
6
7
8
RewriteEngine on
 
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
 
RewriteRule ^([^/]+)/?$ index.php?folder=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?folder=$1&name=$2 [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?folder=$1&name=$2&revision=$3 [NC]

CSS3 selector with jQuery to alter css on even li tags:

$("li:nth-child(even)").css({backgroundColor : red, border : 1px solid black"});

CSS3 selectors with jQuery to select elements based on attribute values:

$("<p>I start with a<\/p>").insertBefore("img[src^='a']");
 
$("<p>I contain with a<\/p>").insertBefore("img[src*='a']");
 
$("<p>I end with a<\/p>").insertBefore("img[src$='a']");

Get the levenshtein distance between two strings:

echo levenshtein ( "zevan" , "kevin" );
// result is 2

Get the metaphone key for a string:

echo metaphone("zevan");
// result is SFN

I have a bunch more of these that I’ll post either over the weekend or on monday.