我正在尝试用下划线替换所有空格,以下内容不起作用:
$id = "aa aa";
echo $id;
preg_replace('/\s+/', '_', $id);
echo $id;
Run Code Online (Sandbox Code Playgroud)
版画
aa aaaa aa
Run Code Online (Sandbox Code Playgroud) 如何在Yii应用程序的任何位置调用这些库函数?我有一个图书馆:
#mylib.php
<?php
class MyLib {
public function foo()
{
echo "hello!";
}
}
Run Code Online (Sandbox Code Playgroud)
并希望能够在我的Yii应用程序中调用此功能:
MyLib::foo();
Run Code Online (Sandbox Code Playgroud)
我不知道在哪里放置我的图书馆或如何/在哪里导入它.这只是我正在尝试做的一个例子,但我正在尝试创建一个具有多个名称空间的库,以便我可以访问该库并在导入后访问所有名称空间.
我有一个用C#编写的客户端和一个用python编写的服务器.我通过套接字发送的消息是8个字节,后跟数据,8个字节是数据长度.
在发送之前的C#中,我将8字节数据长度转换为太大的endian,如图所示:
public void Send(SSLMsg m)
{
string json = m.Serialize();
byte[] data = Encoding.ASCII.GetBytes(json);
ulong dataLen = (ulong)data.Length;
byte[] dataLenPacked = packIt(dataLen);
Log("Sending " + dataLen + " " + json);
sslStream.Write(dataLenPacked);
sslStream.Write(data);
sslStream.Flush();
}
private byte[] packIt(ulong n)
{
byte[] bArr = BitConverter.GetBytes(n);
if (BitConverter.IsLittleEndian)
Array.Reverse(bArr, 0, 8);
return bArr;
}
Run Code Online (Sandbox Code Playgroud)
消息发送成功,我在python服务器代码中被绑定,因为unpack格式应该是正确的,不应该吗?
(length,) = unpack('>Q', data)
# len(data) is 8 here
# length is 1658170187863248538
Run Code Online (Sandbox Code Playgroud)
不是big-endian字符'>'?为什么我的长度太长了?
有一个错误,我正在解包错误的8个字节,已经修复,现在我正在解包正确的数据,我仍然有相同的问题.
(length,) = unpack('>Q', data)
# len(data) is 8 here
# …Run Code Online (Sandbox Code Playgroud) 给定了一些字节,它将其格式化为"字节","KB","MB"或"GB"......但我不明白的是部分:
$_->[1], $_->[0]
Run Code Online (Sandbox Code Playgroud)
是不是传递给地图只是一个哈希数组?那么如何才能有0和1指数呢?
sub fmt {
my $bytes = shift;
return (
sort { length $a <=> length $b }
map { sprintf '%.3g%s', $bytes/1024**$_->[1], $_->[0] }
[" bytes"=>0],[KB=>1],[MB=>2],[GB=>3]
)[0];
}
Run Code Online (Sandbox Code Playgroud) 我需要在内存中存储一个“大”文件(比如说不到2mb),我想知道是否可以使用
char buffer[2048000];
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来处理它?使用大型char数组是否有任何性能问题?
我有下表:
tableA
+-----------+--------+
| tableA_id | code |
+-----------+--------+
| 1 | code A |
| 2 | code B |
| 3 | code A |
| 3 | code C |
| 3 | code B |
| 4 | code A |
| 4 | code C |
| 4 | code B |
| 5 | code A |
| 5 | code C |
| 5 | code B |
+-----------+--------+
Run Code Online (Sandbox Code Playgroud)
我想使用查询来显示代码 A、代码 B、代码 C …
只是想知道这样投射是否安全:
char **cpp;
// ... allocation and what not
void *vp = (void *)cpp;
// ...
cpp = (char **)vp;
Run Code Online (Sandbox Code Playgroud)
应该使用无效**还是无效*罚款?这对我的几个盒子没有问题,但是想知道它是否会在某些系统上引起问题.
以下应用程序适用于已注释掉的malloced int和仅使用int指针指向本地int"a"时.我的问题是如果没有malloc这样做是否安全,因为当函数'doit'返回时,我认为int'a'超出范围,而int*p指向什么都没有.该程序是否由于其简单性而不是错误的或者这是完全正常的吗?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct ht {
void *data;
} ht_t;
ht_t * the_t;
void doit(int v)
{
int a = v;
//int *p = (int *) malloc (sizeof(int));
//*p = a;
int *p = &a;
the_t->data = (void *)p;
}
int main (int argc, char *argv[])
{
the_t = (ht_t *) malloc (sizeof(ht_t));
doit(8);
printf("%d\n", *(int*)the_t->data);
doit(4);
printf("%d\n", *(int*)the_t->data);
}
Run Code Online (Sandbox Code Playgroud) 这就是我想要的,但我不记得以前在任何地方看过它,并且想知道它是不确定的行为还是正确的.我指的是我如何将短句传递给f().
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void f(short *s)
{
printf("%d\n",s[0]);
}
int main(int argc, char *argv[])
{
f((short[5]){0,1,1,1,1});
}
Run Code Online (Sandbox Code Playgroud) 这是什么原因?我认为如果指针为null,则不会评估其余条件.
// doesn't work:
char *ptr = somefunction();
if (ptr && ptr[0] == '1' || ptr[0] == 't')
// ...
// does work:
char *ptr = somefunction();
if (ptr)
if (ptr[0] == '1' || ptr[0] == 't')
// ...
Run Code Online (Sandbox Code Playgroud) 我有以下html/css.当我离开边框时,一切都正确排列,但是如果我将边框放在中心div上,那么右边的内容会显示在其他所有内容之下.我怎样才能解决这个问题?
#container {} #left {
width: 20%;
float: left;
}
#center {
width: 60%;
float: left;
border-left: 1px solid black; /*this causes the 'right' div to wrap*/
border-right: 1px solid black; /*this causes the 'right' div to wrap*/
}
#right {
float: left;
width: 20%;
}Run Code Online (Sandbox Code Playgroud)
<div id="container">
<div id="left">I am left</div>
<div id="center">I am center</div>
<div id="right">I am right</div>
<div style="clear: both;"></div>
</div>Run Code Online (Sandbox Code Playgroud)
以下输出是"str-3",但我希望它是"str-2,str-3",
如果我将snprintf更改为sprintf,那么这就是我预期的方式; 不应该snprintf和sprintf表现一样吗?
int main (int argc, char **argv)
{
char str[64];
str[0] = '\0';
snprintf(str, 64, "%s%s,", str, "str-2");
snprintf(str, 64, "%s%s,", str, "str-3");
printf("%s\n",str);
}
Run Code Online (Sandbox Code Playgroud) Perl中是否有一个很好的衬里,可以让你从字符串中创建一个唯一值数组?
$str = "bob-2-4 asdfasdfasdf bob-2-4 asdfasdf bob-3-1";
my @unique = $str =~ m/(bob-\d-\d)/g;
# array is now "bob-2-4, bob-2-4, bob-3-1"
Run Code Online (Sandbox Code Playgroud)
我希望这个独特的阵列只包含"bob-2-4,bob-3-1".