php中利用explode函数分割字符串到数组

Freya ·
更新时间:2024-09-21
· 522 次阅读

分割字符串
//利用 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;
您可能感兴趣的文章:php获取数组长度的方法(有实例)PHP中数组合并的两种方法及区别介绍判断PHP数组是否为空的代码PHP从数组中删除元素的四种方法实例php数组添加元素方法小结PHP遍历数组的方法汇总php数组(array)输出的三种形式详解PHP遍历数组的几种方法PHP获得数组交集与差集的方法PHP中数组定义的几种方法PHP数组基本用法与知识点总结



字符串 分割字符串 explode PHP 数组 字符

需要 登录 后方可回复, 如果你还没有账号请 注册新账号