Archiv der Kategorie ‘php‘

 
 

Cap Sentence

It’s odd sometimes the code that comes one when your tired:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function sentenceCap($str){
    $alphanumeric = "/[a-zA-Z0-9]/";
    $period = "/\./";
    $count = strlen($str);
    $newString = $str[0];
    for($i = 1; $i < $count; $i++){
      $curr = $str[$i];
      if (preg_match($period, $curr)){
        $nextCap  = true;
      }
      if(preg_match($alphanumeric, $curr)){
        if (!$nextCap){
          $curr = strtolower($curr);
        }else{
          $nextCap = false; 
        }
      }
      $newString .= $curr;
    } 
    return $newString;
  }

5 minutes on php.net… no luck… skrew it… 5 minutes of coding this absurd function. At least I can replace the implementation in the morning when I think of a better way to do it.

EDIT:
I didn’t have time to clean up the function yet but Evan Frohlich posted this really nice solution:

1
2
3
4
5
6
7
8
9
function sentenceCap($str){
    return preg_replace('/([.!?])\s*(\w)/e', "strtoupper('\\1 \\2')", ucfirst(strtolower($str)));
}
 
//Also regarding the above I think you should shift all of your strings 
//to Upper Case first. Otherwise un-capitalized words that begin a sentence will not be corrected.
 
//Line 4 should be:
$curr = strtoupper($str[$i]);

Replace Single Word or String in MYSQL

Someone asked me this one today on IM right before they were about to write a php script for it.

“How do you replace a single word/substring in a given field of a table?”

Here is how it’s done:

UPDATE theTable SET theField = REPLACE(theField, "replace me", "with something else");

Easy enough, no reason to write nasty php to loop through each row etc…

Great CDATA Trick – (Multiple Line String Literal)

One thing I like about php strings is that you can easily write them on multiple lines:

1
2
3
4
5
6
$theString = "
<!DOCTYPE html>
  <html>
    <head>
      <meta charset='utf-8' /> 
etc....";

It always bugged me that you couldn’t do this in actionscript. Turns out you can – I did some googling the other day in an attempt to find a way – and to my surprise I found this question on stackoverflow note the answer by Nick Sonneveld.

Here’s how it works:

txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();

It’s usually the case that you want to use condenseWhite to remove extra whitespace – however being able to preserve returns and extra spacing may also be desirable.

I used this because I was manually copy and pasting information from an html version of a word doc – clicking a button in an air app I wrote to remove SOME of the html elements and attributes like and class=”MSsomething” and then pasting the final result into some weird timeline based flash templates (that someone else created). This little snippet saved me some serious time. The air app also did a lot of the work with some regular expressions. Converting things like:

<p>.           stuff</p>

to…

<li>stuff</li>

A funny side note: My syntax highlighter did NOT dig this AS syntax, so to get it to show up in a readable way, I wrote some php directly in the post (I have Exec-PHP installed);

1
2
3
4
5
6
7
8
9
10
11
<?php
echo htmlentities("txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();
");
?>