Archive for 'Strings'

Using Eval

I use this function very rarely. For some reason I just never need to use it. I know in Drupal it is used quite often, especially if you use the PHP input field for a node. This allows you to enter PHP code that will be evaluated (or run) and then the output is shown as the node’s content.

I suppose this is mostly used when you want a user to be able to enter PHP code into a text field and then run it. Not always too secure I would think, so be careful where and how you use this.

  $s = "<?php phpinfo(); ?>";
  eval($s);

Does a string begin with another string

Here is a function to check if a string begings with another string

  function startsWith($haystack,$needle,$case=true) {
    if($case)  { 
      return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);
    }
    return (strcmp(strtolower(substr($haystack, 0, strlen($needle))),strtolower($needle))===0);
  }

Does a string end with another string

Here’s a nice function to check if a string ends with another string:

  function endsWith($haystack,$needle,$case=true) {
    if($case){
      return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
    }
    return (strcmp(strtolower(substr($haystack, strlen($haystack) - strlen($needle))),strtolower($needle))===0);
  }

Quick and dirty template system

Before I knew about Smarty, I developed me own way of having HTML templates for the projects I developed. Admittedly, I still use it even though Smarty is so much more powerful. When I don’t need the versatility of Smarty, I just use my own method.

I started off by having a template directory with HTML files in it. These files consist of all the HTML code I want, and has placeholders to where content will be.

<html>
<body>
<div>%header%</div>
<div>
  <div>%menu%</div>
  <div>%content%</div>
</div>
<div>%footer%</div>
</body>
</html>

As you can see, I have 4 place holders there, %header%, %menu%, %content% and %footer.

Now all I have to do is use str_replace to replace the place holders with content.

Here is an example of doing that

  $html = file_get_contents("template.html");
 
  $menu = "<ul><li>Option 1</li><li>Option 2</li></ul>";
  $header = "<h1>This is the Header</h1><br />%menu%";
  $content = "<p>This is some content.</p>";
  $footer = "<small>This is the footer</small>";
 
  $html = str_replace("%header%",$header,$html);
  $html = str_replace("%menu%",$menu,$html);
  $html = str_replace("%content%",$content,$html);
  $html = str_replace("%footer%",$footer,$html);
  echo $html;

As you can see, I make use of the placeholder within another placeholder as well. So the %menu% is also replace within the $header variable. Just note if you want to use it like that, you need to order the str_replace so that it will get replaced.

The resulting HTML will be:

<html>
<body>
<div><h1>This is the Header</h1><br /><ul><li>Option 1</li><li>Option 2</li></ul></div>
<div>
  <div><ul><li>Option 1</li><li>Option 2</li></ul></div>
  <div><p>This is some content.</p></div>
</div>
<div><small>This is the footer</small></div>
</body>
</html>

Strlen and Isset for string length

If you were asked to check up on the length of string, you’d probably use the following code:

$str = "This is a string";
echo strlen($str);

Let’s look at a different way of doing this:

$str = 'This is a string';
if (isset($str[9])) {
    echo 'The input is longer or equal then 10 characters.';
} else  {
    echo 'The input is less then 10 characters long.';
}

The equivalent with strlen:

$str = 'This is a string';
if (strlen($str) >= 10) {
    echo 'The input is longer or equal then 10 characters.';
} else {
    echo 'The input is less then 10 characters long.';
}

Now, why use isset instead of strlen? I’ve read documents claiming that isset runs up to 5 time faster than strlen in most cases. But that is not the main reason for wanting to use it. Try using strlen on a variable that has not been initialized yet. Yes, I know, that is what PHP and languages like it has done: allow people to use uninitialized variables. And although I believe it to be bad programming practice to not initial variables, it happens. Getting back to using strlen on an uninitialized variable. You will get a “Notice” error telling you that it is not yet initialized. Although not a major problem, it is an irritation seeing these notices if you have them enabled.

So, start using isset if you want a bit more performance or start remember to initialize variables and use strlen.