我决定使用favs(标记该帖子作为收藏的用户的id)作为favs列中的逗号分隔列表,该列也在发送者,网址,内容等的消息表中.
但是,当我尝试用以下查询计算这些行时:
select count(id)
from messages
where favs like '%userid%'
Run Code Online (Sandbox Code Playgroud)
当然它会返回错误的结果,因为所有id都可能是另一个id的一部分
例如,在查询id = 1时,它还会增加用户ID 11所支持的任何其他内容的计数器...
你能告诉我你的想法或任何使这个系统有效的解决方案吗?
有没有办法在没有任何空格的情况下分解指定变量中的值?
例如.
$var = 123456789;
Run Code Online (Sandbox Code Playgroud)
我可以通过爆炸阵列购买来做到这一点,因为没有空间我有困难.
$result = explode("", $var);
$results[0] = $a;
$result[1] = $b;
$result[2] = $c;
Run Code Online (Sandbox Code Playgroud)
等等...
$a would = 1
$b would = 2
$c would = 3
Run Code Online (Sandbox Code Playgroud)
等等...
这可能吗?
<?php
$string = "qwertyuiopasdfghjklzxcvbnm";
$string = explode("", $string);
sort($string);
foreach ($string as $val) {
echo $val."<br>";
}
?>
Run Code Online (Sandbox Code Playgroud)
我想要输出:a b c ...但是怎么样?
我有一个字符串
"myhashkey?key1=val1&key2=val2&key3=val3&key4=val4"
Run Code Online (Sandbox Code Playgroud)
我想要爆炸
myhashkey => {
key1 => val1,
key2 => val2,
key3 => val3
}
Run Code Online (Sandbox Code Playgroud)
我也想把它折回到同一个字符串.
到目前为止,我提出的相当混乱,使用索引并尝试手动构建值
$arg = $_[0];
#if arg has = it may be key=val string
if(index($arg,'=') > -1 ){
#if arg has & character it might be key=val&key1=val
if(index($arg,'&') > -1 ){
#$arg =~ m/[=&\?]/
@r = split(/[=&\?]/,$arg);
my $hashkey = shift(@r)
my %values = @r;
return $class->$orig( key => $k, $value => \%values );
...
}else{
@r = split('=',$arg);
return ( key => $r[0], …Run Code Online (Sandbox Code Playgroud) 我怎么能爆炸数组中的每个元素?
Array
(
[0] => countryId:2, productId:1, status:1, pId:18, xId:pdgje5566
)
Run Code Online (Sandbox Code Playgroud)
请看以下数组:
$searchfor = $xId;
header('Content-Type: text/plain');
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
print_r($matches[0]);
//echo implode("\n", $matches[0]);
}
Run Code Online (Sandbox Code Playgroud)
请指教,谢谢.
我想要对文本进行子串,例如."你好,你好吗?" 通过使用此代码
$text= "Hello - How are you?";
$strings = explode(' - ',$text);
echo $strings[0]; // Hello
echo $strings[1]; // How are you
Run Code Online (Sandbox Code Playgroud)
它不会起作用,因为' - '.
如果我改为:
$text= "Hello-How are you?";
$strings = explode('-',$text);
echo $strings[0]; // Hello
echo $strings[1]; // How are you
Run Code Online (Sandbox Code Playgroud)
没关系.
有人能帮我吗?谢谢.
这可能看起来微不足道,但它伤害了我的头脑.有人可以解释原因http://php.net/manual/es/function.explode.php示例显示(我已经修剪过).
$pizza = "piece1 piece2 piece3"; // string
$pieces = explode(" ", $pizza);
// after a var dump
array(3) { [0]=> string(6) "piece1"
[1]=> string(6) "piece2"
[2]=> string(6) "piece3" }
Run Code Online (Sandbox Code Playgroud)
这很可爱,删除了所有的空间并做了一个很好的小数组,但是当我使用下面显示的类似的东西时
$path = "/test-gallery/2/"; // string
$urlpieces = explode("/", $path);
// after a var dump
array(4) { [0]=> string(0) ""
[1]=> string(12) "test-gallery"
[2]=> string(1) "2"
[3]=> string(0) "" }
Run Code Online (Sandbox Code Playgroud)
我得到第一个和最后一个空字符串.为什么不删除第一个和最后一个数组元素?我总是可以添加另一个步骤并删除它,但爆炸应该把它全部拿出来不应该吗?
在此先感谢您的建议.
我想从另一个地方创建一个字符串:
1-第一个字符串是:"4-3 | 5-2 | 9-6 | 7-1 | 2-8"
2-新字符串必须是:"4,5,9,7,2"
我有这段代码,但它不起作用:
$string_1 = '4-3|5-2|9-6|7-1|2-8';
$array_1 = explode('|', $string_1);
$string_2 = '';
foreach ( $array_1 as $item ) {
$array_2 = explode('-', $item);
foreach ( $array_2 as $item_id => $item_value ) {
$string_2 .= ($string_2 == '') ? $item_id : ', ' . $item_id;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试按字母顺序对字符串进行排序.我以为我可以将一个字符串分解为一个数组并对其进行排序,但是回声没有返回任何内容.
$schools = "high*low*other*";
$schools = explode("*", $schools);
$schools = sort($schools);
echo $schools[0];
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个if语句.基于的字符串
$var = "Apple : Banana";
$array = explode(":", $var);
print_r($array); //array([0] => 'Apple' [1] => 'Banana')
if ($array[1] == "Banana") {
echo "Banana!";
}
Run Code Online (Sandbox Code Playgroud)