Web Dev
[Linux/Shell/Bash] Count pattern matches/instances in string
by Chuck Kozler on Jan.20, 2010, under Computers, Linux/Unix, Networks/Servers, Programming, Web Dev
This handy little Bash scripting function will count the number of instances it finds in a string by the given delimeter. As of right now, its pretty basic…I didnt go too far into expanding it and working it (like going through and escaping all character escapes) and only escape for | and \.
function countPatterns { string_in="$1" delim="$2" if [ $delim == "/" ] || [ $delim == "|" ]; then delim="\\$delim"; fi retval=`echo $string_in | perl -ne 'while(/'${delim}'/g){++$count}; print "$count\n"'` return $retval }
Remember, this isnt really completed but you get the idea. The use is something like:
countPatterns "this|is|a|string|" "|" instances="$?" #would output 4 echo $instances
[PHP]Empty an array
by Chuck Kozler on Dec.20, 2009, under Computers, Home, Programming, Web Dev
This simple function will empty an array for you. I had to use this for some stuff I was writing for parsing the results of a MySQL query.
I am surprised that there isnt a built in PHP function for this. If there is, let me know…please!
<?php function emptyArray( $array ) { $size = sizeof( $array ); for( $i = 0; $i < $size; $i++ ) { array_pop( $array ); } } ?>
I would use this so that I could use the same array over and over again in a class and have it be dumped every execution of the function it was being utilized in.
Sphere: Related Content[PHP] Kill MySQL Connections
by Chuck Kozler on Jan.08, 2009, under Computers, Networks/Servers, Web Dev
Here is some code I made in PHP to kill all concurrent MySQL connections. I made this one night because a MySQL command I needed to kill commands could not handle wild cards. This iterates through all connections/processes and kills them one by one. If you have some knowledge of PHP then this is self-explanatory:
<?php $con = mysql_connect( "localhost", "root", "<password>" ) or die( "can not connect" ); if( $con ) echo "Connected<br>"; $result = mysql_query( "SHOW FULL PROCESSLIST", $con ); while( $row = mysql_fetch_array( $result, $con ) ) { $process_id = $row["id"]; if( $row["Info"] == "NULL" ) { $sql = "KILL $process_id"; $res = mysql_query( "$sql", $con ); if( $res ) { echo "Mysql Process ID $process_id has been killed<br>"; } } else echo "Row not found?<br>"; } ?>