PHP是一種通用的開(kāi)源腳本語(yǔ)言。PHP的獨(dú)特語(yǔ)法是C,Java,Perl和PHP自己的語(yǔ)法的混合。它可以比CGI或Perl更快地執(zhí)行動(dòng)態(tài)網(wǎng)頁(yè)。與其他編程語(yǔ)言相比,用PHP創(chuàng)建的動(dòng)態(tài)頁(yè)面是通過(guò)將程序嵌入HTML文檔中來(lái)執(zhí)行的,其執(zhí)行效率比完全生成HTML標(biāo)簽的CGI更高。它可以嵌入HTML中,特別適合于Web開(kāi)發(fā)。那么php socket如何設(shè)置超時(shí)?php字符串太長(zhǎng)怎么辦?中培專(zhuān)家為您詳解。
php socket如何設(shè)置超時(shí)?
//如果$waitAckSec=0,則返回成功發(fā)送的字節(jié)
//如果$waitAckSec大于0,則返回發(fā)送后接收到得內(nèi)容
//任何情況下,失敗都返回FALSE
function sendUdp($host, $port, $buff,$waitAckSec=0) {
$socket = ($result = @socket_create(AF_INET,SOCK_DGRAM,SOL_UDP));
//發(fā)送超時(shí)1秒
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) );
//接收超時(shí)6秒
socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>6, "usec"=>0 ) );
if($socket){
$result = @socket_sendto($socket,$buff,strlen($buff),0,$host,$port);
if($waitAckSec>0){
$result = FALSE;
$read = array($socket);
$write = NULL;
$except = NULL;
if(@socket_select($read,$write,$except,$waitAckSec)>0){
$fromHost = "";
$fromPort = 0;
@socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort);
$result = phpext_unpack($result);
if($result["needAck"] == 1){
$this->sendUdp($host, $port, $result["ackdata"]);
if(isset ($result['data']['list']) && isset ($result['data']['totalCount'])){
$list = $result['data']['list'];
$count = $result['data']['totalCount'];
while($count> count($list)){
@socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort);
$result_temp = phpext_unpack($result_temp);
$this->sendUdp($host, $port, $result_temp["ackdata"]);
$list = array_merge($list,$result_temp['data']['list']);
}
$result['data']['list'] = $list;
}
}else{
@socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort);
$result = phpext_unpack($result);
if($result["needAck"] == 1){
$this->sendUdp($host, $port, $result["ackdata"]);
if(isset ($result['data']['result']) && isset ($result['data']['userID'])){
$list = $result['data']['list'];
$count = $result['data']['totalCount'];
while($count> count($list)){
@socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort);
$result_temp = phpext_unpack($result_temp);
$this->sendUdp($host, $port, $result_temp["ackdata"]);
$list = array_merge($list,$result_temp['data']['list']);
}
$result['data']['list'] = $list;
}
}
}
}else{
$result = SEND_UDP_ERROR;
}
}
@socket_close($socket);
}
return $result;
}
php字符串太長(zhǎng)怎么辦?
php字符串太長(zhǎng)的解決辦法:首先截取長(zhǎng)度等于0或大于等于本字符串的長(zhǎng)度,則返回字符串本身;然后如果截取長(zhǎng)度為負(fù)數(shù),那么截取長(zhǎng)度就等于字符串長(zhǎng)度減去截取長(zhǎng)度;最后如果截取長(zhǎng)度的絕對(duì)值大于字符串本身長(zhǎng)度,則截取長(zhǎng)度取字符串本身的長(zhǎng)度。
if (! function_exists('mbSubStr')){
function mbSubStr($str, $length = 0, $append = true)
{
$str = trim($str);
$strlength = strlen($str);
if ($length == 0 || $length >= $strlength) {
return $str; //截取長(zhǎng)度等于0或大于等于本字符串的長(zhǎng)度,返回字符串本身
}elseif ($length < 0){ //如果截取長(zhǎng)度為負(fù)數(shù)
$length = $strlength + $length;//那么截取長(zhǎng)度就等于字符串長(zhǎng)度減去截取長(zhǎng)度
if ($length < 0) {
$length = $strlength;//如果截取長(zhǎng)度的絕對(duì)值大于字符串本身長(zhǎng)度,則截取長(zhǎng)度取字符串本身的長(zhǎng)度
}
}
if (function_exists('mb_substr')){
$newstr = mb_substr($str, 0, $length, 'utf-8');
}elseif (function_exists('iconv_substr')){
$newstr = iconv_substr($str, 0, $length, 'utf-8');
}else{
//$newstr = trim_right(substr($str, 0, $length));
$newstr = substr($str, 0, $length);
}
if ($append && $str != $newstr){
$newstr .= '...';
}
return $newstr;
}
}
以上就是關(guān)于php socket如何設(shè)置超時(shí),以及php字符串太長(zhǎng)怎么辦的全部?jī)?nèi)容,想了解更多關(guān)于php的信息,請(qǐng)繼續(xù)關(guān)注中培偉業(yè)。