php中利用explode函数分割字符串到数组
更新时间:2014年02月08日 15:54:51 作者:
这篇文章主要介绍了php中利用explode函数分割字符串到数组,需要的朋友可以参考下
分割字符串
//利用 explode 函数分割字符串到数组
<?php
$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串
$hello = explode(',',$source);
for($index=0;$index<count($hello);$index++)
{
echo $hello[$index];echo "</br>";
}
?>
//split函数进行字符分割
// 分隔符可以是斜线,点,或横线
<?php
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>
通过数组实现多条件查询的代码
<?php
$keyword="asp php,jsp";
$keyword=str_replace(" "," ",$keyword);
$keyword=str_replace(" ",",",$keyword);
$keyarr=explode(',',$keyword);
for($index=0;$index<count($keyarr);$index++)
{
$whereSql .= " And (arc.title like '%$keyarr[$index]%' Or arc.keywords like '%$keyarr[$index]%') ";
}
echo $whereSql;
//利用 explode 函数分割字符串到数组
复制代码 代码如下:
<?php
$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串
$hello = explode(',',$source);
for($index=0;$index<count($hello);$index++)
{
echo $hello[$index];echo "</br>";
}
?>
//split函数进行字符分割
// 分隔符可以是斜线,点,或横线
复制代码 代码如下:
<?php
$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>
通过数组实现多条件查询的代码
复制代码 代码如下:
<?php
$keyword="asp php,jsp";
$keyword=str_replace(" "," ",$keyword);
$keyword=str_replace(" ",",",$keyword);
$keyarr=explode(',',$keyword);
for($index=0;$index<count($keyarr);$index++)
{
$whereSql .= " And (arc.title like '%$keyarr[$index]%' Or arc.keywords like '%$keyarr[$index]%') ";
}
echo $whereSql;
相关文章
详解WordPress中调用评论模板和循环输出评论的PHP函数
这篇文章主要介绍了WordPress中调用评论模板和循环输出评论的PHP函数,分别是comments_template函数与wp_list_comments函数的使用,需要的朋友可以参考下2016-01-01
深思 PHP 数组遍历的差异(array_diff 的实现)
还是部门无聊的考题,不过这次考的是 PHP 的能力。题目如下: 给你两个分别有 5000 个元素的数组,计算他们的差集 -- 说白了也就是用 PHP 和你认为最好的算法实现 array_diff 的算法。初次接到这个题目,我发现这非常的简单,于是按照以往的经验“随便”写了一个:2008-03-03


最新评论