当前位置: 编程技术>php
本页文章导读:
▪用PHP写的基于Memcache的Queue实现代码
php类代码: 代码如下: <?php class MQ{ public static $client; private static $m_real; private static $m_front; private static $m_data = array(); const QUEUE_MAX_NUM = 100000000; const QUEUE_FRONT_KEY = '_queue_item_front'; const QUEUE_REAL_.........
▪PHP中去除换行解决办法小结(PHP_EOL)
第一种写法: $content=str_replace("\n","",$content); echo $content; 第二种写法: str_replace("\r\n","",$str); 第三种写法: $content=preg_replace("/\s/","",$content); echo $content; 附: 首先说说\n,\r,\t \n 软回车: 在Windows 中.........
▪php操作SVN版本服务器类代码
SvnPeer.php 代码如下: <?php /** * * This class for execute the external program of svn * * @auth Seven Yang <qineer@gmail.com> * */ class SvnPeer { /** * List directory entries in the repository * * @param string a specific project rep.........
[1]用PHP写的基于Memcache的Queue实现代码
来源: 互联网 发布时间: 2013-11-30
php类代码:
<?php
class MQ{
public static $client;
private static $m_real;
private static $m_front;
private static $m_data = array();
const QUEUE_MAX_NUM = 100000000;
const QUEUE_FRONT_KEY = '_queue_item_front';
const QUEUE_REAL_KEY = '_queue_item_real';
public static function setupMq($conf) {
self::$client = memcache_pconnect($conf);
self::$m_real = memcache_get(self::$client, self::QUEUE_REAL_KEY);
self::$m_front = memcache_get(self::$client, self::QUEUE_FRONT_KEY);
if (!isset(self::$m_real) || emptyempty(self::$m_real)) {
self::$real= 0;
}
if (!isset(self::$m_front) || emptyempty(self::$m_front)) {
self::$m_front = 0;
}
return self::$client;
}
public static function add($queue, $data) {
$result = false;
if (self::$m_real < self::QUEUE_MAX_NUM) {
if (memcache_add(self::$client, $queue.self::$m_real, $data)) {
self::mqRealChange();
$result = true;
}
}
return $result;
}
public static function get($key, $count) {
$num = 0;
for ($i=self::$m_front;$i<self::$m_front + $count;$i++) {
if ($dataTmp = memcache_get(self::$client, $key.$i)) {
self::$m_data[] = $dataTmp;
memcache_delete(self::$client, $key.$i);
$num++;
}
}
if ($num>0) {
self::mqFrontChange($num);
}
return self::$m_data;
}
private static function mqRealChange() {
memcache_add(self::$client, self::QUEUE_REAL_KEY, 0);
self::$m_real = memcache_increment(self::$client, self::QUEUE_REAL_KEY, 1);
}
private static function mqFrontChange($num) {
memcache_add(self::$client, self::QUEUE_FRONT_KEY, 0);
self::$m_front = memcache_increment(self::$client, self::QUEUE_FRONT_KEY, $num);
}
public static function mflush($memcache_obj) {
memcache_flush($memcache_obj);
}
public static function Debug() {
echo 'real:'.self::$m_real."<br>/r/n";
echo 'front:'.self::$m_front."<br>/r/n";
echo 'wait for process data:'.intval(self::$m_real - self::$m_front);
echo "<br>/r/n";
echo '<pre>';
print_r(self::$m_data);
echo '<pre>';
}
}
define('FLUSH_MQ',0);//CLEAN ALL DATA
define('IS_ADD',0);//SET DATA
$mobj = MQ::setupMq('127.0.0.1','11211');
if (FLUSH_MQ) {
MQ::mflush($mobj);
} else {
if (IS_ADD) {
MQ::add('user_sync', '1test');
MQ::add('user_sync', '2test');
MQ::add('user_sync', '3test');
MQ::add('user_sync', '4test');
MQ::add('user_sync', '5test');
MQ::add('user_sync', '6test');
} else {
MQ::get('user_sync', 10);
}
}
MQ::Debug();
?>
使用方法
MQ::setupMq('127.0.0.1','11211');//连接
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ:get($key, 10);//取出一定数量的数据
代码如下:
<?php
class MQ{
public static $client;
private static $m_real;
private static $m_front;
private static $m_data = array();
const QUEUE_MAX_NUM = 100000000;
const QUEUE_FRONT_KEY = '_queue_item_front';
const QUEUE_REAL_KEY = '_queue_item_real';
public static function setupMq($conf) {
self::$client = memcache_pconnect($conf);
self::$m_real = memcache_get(self::$client, self::QUEUE_REAL_KEY);
self::$m_front = memcache_get(self::$client, self::QUEUE_FRONT_KEY);
if (!isset(self::$m_real) || emptyempty(self::$m_real)) {
self::$real= 0;
}
if (!isset(self::$m_front) || emptyempty(self::$m_front)) {
self::$m_front = 0;
}
return self::$client;
}
public static function add($queue, $data) {
$result = false;
if (self::$m_real < self::QUEUE_MAX_NUM) {
if (memcache_add(self::$client, $queue.self::$m_real, $data)) {
self::mqRealChange();
$result = true;
}
}
return $result;
}
public static function get($key, $count) {
$num = 0;
for ($i=self::$m_front;$i<self::$m_front + $count;$i++) {
if ($dataTmp = memcache_get(self::$client, $key.$i)) {
self::$m_data[] = $dataTmp;
memcache_delete(self::$client, $key.$i);
$num++;
}
}
if ($num>0) {
self::mqFrontChange($num);
}
return self::$m_data;
}
private static function mqRealChange() {
memcache_add(self::$client, self::QUEUE_REAL_KEY, 0);
self::$m_real = memcache_increment(self::$client, self::QUEUE_REAL_KEY, 1);
}
private static function mqFrontChange($num) {
memcache_add(self::$client, self::QUEUE_FRONT_KEY, 0);
self::$m_front = memcache_increment(self::$client, self::QUEUE_FRONT_KEY, $num);
}
public static function mflush($memcache_obj) {
memcache_flush($memcache_obj);
}
public static function Debug() {
echo 'real:'.self::$m_real."<br>/r/n";
echo 'front:'.self::$m_front."<br>/r/n";
echo 'wait for process data:'.intval(self::$m_real - self::$m_front);
echo "<br>/r/n";
echo '<pre>';
print_r(self::$m_data);
echo '<pre>';
}
}
define('FLUSH_MQ',0);//CLEAN ALL DATA
define('IS_ADD',0);//SET DATA
$mobj = MQ::setupMq('127.0.0.1','11211');
if (FLUSH_MQ) {
MQ::mflush($mobj);
} else {
if (IS_ADD) {
MQ::add('user_sync', '1test');
MQ::add('user_sync', '2test');
MQ::add('user_sync', '3test');
MQ::add('user_sync', '4test');
MQ::add('user_sync', '5test');
MQ::add('user_sync', '6test');
} else {
MQ::get('user_sync', 10);
}
}
MQ::Debug();
?>
使用方法
代码如下:
MQ::setupMq('127.0.0.1','11211');//连接
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ::add($key, $value);//添加数据到队列
MQ:get($key, 10);//取出一定数量的数据
[2]PHP中去除换行解决办法小结(PHP_EOL)
来源: 互联网 发布时间: 2013-11-30
第一种写法:
$content=str_replace("\n","",$content);
echo $content;
第二种写法:
str_replace("\r\n","",$str);
第三种写法:
$content=preg_replace("/\s/","",$content);
echo $content;
附:
首先说说\n,\r,\t
\n 软回车:
在Windows 中表示换行且回到下一行的最开始位置
在Linux、unix 中只表示换行,但不会回到下一行的开始位置。
\r 软空格:
在Linux、unix 中表示返回到当行的最开始位置。
在Mac OS 中表示换行且返回到下一行的最开始位置,相当于Windows 里的 \n 的效果。
\t 跳格(移至下一列)
几点说明:
它们在双引号或定界符表示的字符串中有效,在单引号表示的字符串中无效。
\r\n 一般一起用,用来表示键盘上的回车键(Linux,Unix中),也可只用 \n(Windwos中),在Mac OS中用\r表示回车!
\t表示键盘上的“TAB”键。
文件中的换行符号:
windows : \n
linux,unix: \r\n
实例代码:
<?php
//php 不同系统的换行
//不同系统之间换行的实现是不一样的
//linux 与unix中用 /n
//MAC 用 /r
//window 为了体现与linux不同 则是 /r/n
//所以在不同平台上 实现方法就不一样
//php 有三种方法来解决
//1、使用str_replace 来替换换行
$str = str_replace(array("/r/n", "/r", "/n"), "", $str);
//2、使用正则替换
$str = preg_replace('//s*/', '', $str);
//3、使用php定义好的变量 (建议使用)
$str = str_replace(PHP_EOL, '', $str);
?>
$content=str_replace("\n","",$content);
echo $content;
第二种写法:
str_replace("\r\n","",$str);
第三种写法:
$content=preg_replace("/\s/","",$content);
echo $content;
附:
首先说说\n,\r,\t
\n 软回车:
在Windows 中表示换行且回到下一行的最开始位置
在Linux、unix 中只表示换行,但不会回到下一行的开始位置。
\r 软空格:
在Linux、unix 中表示返回到当行的最开始位置。
在Mac OS 中表示换行且返回到下一行的最开始位置,相当于Windows 里的 \n 的效果。
\t 跳格(移至下一列)
几点说明:
它们在双引号或定界符表示的字符串中有效,在单引号表示的字符串中无效。
\r\n 一般一起用,用来表示键盘上的回车键(Linux,Unix中),也可只用 \n(Windwos中),在Mac OS中用\r表示回车!
\t表示键盘上的“TAB”键。
文件中的换行符号:
windows : \n
linux,unix: \r\n
实例代码:
代码如下:
<?php
//php 不同系统的换行
//不同系统之间换行的实现是不一样的
//linux 与unix中用 /n
//MAC 用 /r
//window 为了体现与linux不同 则是 /r/n
//所以在不同平台上 实现方法就不一样
//php 有三种方法来解决
//1、使用str_replace 来替换换行
$str = str_replace(array("/r/n", "/r", "/n"), "", $str);
//2、使用正则替换
$str = preg_replace('//s*/', '', $str);
//3、使用php定义好的变量 (建议使用)
$str = str_replace(PHP_EOL, '', $str);
?>
[3]php操作SVN版本服务器类代码
来源: 互联网 发布时间: 2013-11-30
SvnPeer.php
<?php
/**
*
* This class for execute the external program of svn
*
* @auth Seven Yang <qineer@gmail.com>
*
*/
class SvnPeer
{
/**
* List directory entries in the repository
*
* @param string a specific project repository path
* @return bool true, if validated successfully, otherwise false
*/
static public function ls($repository)
{
$command = "svn ls " . $repository;
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'non-existent in that revision')) {
return false;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Duplicate something in working copy or repository, remembering history
*
* @param $src
* @param $dst
* @param $comment string specify log message
* @return bool true, if copy successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function copy($src, $dst, $comment)
{
$command = "svn cp $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Remove files and directories from version control
*
* @param $url
* @return bool true, if delete successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function delete($url, $comment)
{
$command = "svn del $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Move and/or rename something in working copy or repository
*
* @param $src string trunk path
* @param $dst string new branch path
* @param $comment string specify log message
* @return bool true, if move successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function move($src, $dst, $comment)
{
$command = "svn mv $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Create a new directory under version control
*
* @param $url string
* @param $comment string the svn message
* @return bool true, if create successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function mkdir($url, $comment)
{
$command = "svn mkdir $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
static public function diff($pathA, $pathB)
{
$output = SvnPeer::runCmd("svn diff $pathA $pathB");
return implode('<br>', $output);
}
static public function checkout($url, $dir)
{
$command = "cd $dir && svn co $url";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strstr($output, 'Checked out revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
static public function update($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}
return $ret[0][0];
}
static public function merge($revision, $url, $dir)
{
$command = "cd $dir && svn merge -r1:$revision $url";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strstr($output, 'Text conflicts')) {
return 'Command: ' . $command .'<br>'. $output;
}
return true;
}
static public function commit($dir, $comment)
{
$command = "cd $dir && svn commit -m'$comment'";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strpos($output, 'Committed revision') || empty($output)) {
return true;
}
return $output;
}
static public function getStatus($dir)
{
$command = "cd $dir && svn st";
return SvnPeer::runCmd($command);
}
static public function hasConflict($dir)
{
$output = SvnPeer::getStatus($dir);
foreach ($output as $line){
if ('C' == substr(trim($line), 0, 1) || ('!' == substr(trim($line), 0, 1))){
return true;
}
}
return false;
}
/**
* Show the log messages for a set of path with XML
*
* @param path string
* @return log message string
*/
static public function getLog($path)
{
$command = "svn log $path --xml";
$output = SvnPeer::runCmd($command);
return implode('', $output);
}
static public function getPathRevision($path)
{
$command = "svn info $path --xml";
$output = SvnPeer::runCmd($command);
$string = implode('', $output);
$xml = new SimpleXMLElement($string);
foreach ($xml->entry[0]->attributes() as $key=>$value){
if ('revision' == $key) {
return $value;
}
}
}
static public function getHeadRevision($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}
return $ret[0][0];
}
/**
* Run a cmd and return result
*
* @param string command line
* @param boolen true need add the svn authentication
* @return array the contents of the output that svn execute
*/
static protected function runCmd($command)
{
$authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir '.SVN_CONFIG_DIR.'.subversion';
exec($command . $authCommand . " 2>&1", $output);
return $output;
}
}
代码如下:
<?php
/**
*
* This class for execute the external program of svn
*
* @auth Seven Yang <qineer@gmail.com>
*
*/
class SvnPeer
{
/**
* List directory entries in the repository
*
* @param string a specific project repository path
* @return bool true, if validated successfully, otherwise false
*/
static public function ls($repository)
{
$command = "svn ls " . $repository;
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'non-existent in that revision')) {
return false;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Duplicate something in working copy or repository, remembering history
*
* @param $src
* @param $dst
* @param $comment string specify log message
* @return bool true, if copy successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function copy($src, $dst, $comment)
{
$command = "svn cp $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Remove files and directories from version control
*
* @param $url
* @return bool true, if delete successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function delete($url, $comment)
{
$command = "svn del $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Move and/or rename something in working copy or repository
*
* @param $src string trunk path
* @param $dst string new branch path
* @param $comment string specify log message
* @return bool true, if move successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function move($src, $dst, $comment)
{
$command = "svn mv $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
/**
* Create a new directory under version control
*
* @param $url string
* @param $comment string the svn message
* @return bool true, if create successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function mkdir($url, $comment)
{
$command = "svn mkdir $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
static public function diff($pathA, $pathB)
{
$output = SvnPeer::runCmd("svn diff $pathA $pathB");
return implode('<br>', $output);
}
static public function checkout($url, $dir)
{
$command = "cd $dir && svn co $url";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strstr($output, 'Checked out revision')) {
return true;
}
return "<br>" . $command . "<br>" . $output;
}
static public function update($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}
return $ret[0][0];
}
static public function merge($revision, $url, $dir)
{
$command = "cd $dir && svn merge -r1:$revision $url";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strstr($output, 'Text conflicts')) {
return 'Command: ' . $command .'<br>'. $output;
}
return true;
}
static public function commit($dir, $comment)
{
$command = "cd $dir && svn commit -m'$comment'";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strpos($output, 'Committed revision') || empty($output)) {
return true;
}
return $output;
}
static public function getStatus($dir)
{
$command = "cd $dir && svn st";
return SvnPeer::runCmd($command);
}
static public function hasConflict($dir)
{
$output = SvnPeer::getStatus($dir);
foreach ($output as $line){
if ('C' == substr(trim($line), 0, 1) || ('!' == substr(trim($line), 0, 1))){
return true;
}
}
return false;
}
/**
* Show the log messages for a set of path with XML
*
* @param path string
* @return log message string
*/
static public function getLog($path)
{
$command = "svn log $path --xml";
$output = SvnPeer::runCmd($command);
return implode('', $output);
}
static public function getPathRevision($path)
{
$command = "svn info $path --xml";
$output = SvnPeer::runCmd($command);
$string = implode('', $output);
$xml = new SimpleXMLElement($string);
foreach ($xml->entry[0]->attributes() as $key=>$value){
if ('revision' == $key) {
return $value;
}
}
}
static public function getHeadRevision($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}
return $ret[0][0];
}
/**
* Run a cmd and return result
*
* @param string command line
* @param boolen true need add the svn authentication
* @return array the contents of the output that svn execute
*/
static protected function runCmd($command)
{
$authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir '.SVN_CONFIG_DIR.'.subversion';
exec($command . $authCommand . " 2>&1", $output);
return $output;
}
}
最新技术文章: