php替换php 字符串替换中多个指定的字符为固定形式

2013年5月 总版技术专家分月排行榜第一
2016年7月 总版技术专家分月排行榜第二2016年3月 总版技术专家分月排行榜第二2015年12月 总版技术专家分月排行榜第二2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
2013年5月 总版技术专家分月排行榜第一
2016年7月 总版技术专家分月排行榜第二2016年3月 总版技术专家分月排行榜第二2015年12月 总版技术专家分月排行榜第二2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
2013年5月 总版技术专家分月排行榜第一
2016年7月 总版技术专家分月排行榜第二2016年3月 总版技术专家分月排行榜第二2015年12月 总版技术专家分月排行榜第二2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
2013年5月 总版技术专家分月排行榜第一
2016年7月 总版技术专家分月排行榜第二2016年3月 总版技术专家分月排行榜第二2015年12月 总版技术专家分月排行榜第二2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
2013年5月 总版技术专家分月排行榜第一
2016年7月 总版技术专家分月排行榜第二2016年3月 总版技术专家分月排行榜第二2015年12月 总版技术专家分月排行榜第二2014年8月 总版技术专家分月排行榜第二2014年7月 总版技术专家分月排行榜第二2013年6月 总版技术专家分月排行榜第二
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。15:03 提问
php去掉字符串的第一个字符
$a = "About us";
处理完后变成
"bout us";
$a 不是固定的
先只考虑英文就好
按赞数排序
$a = "About us";
$a=substr($a,1);
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!
$str = "About us";
$str = substr_replace($str,"",0,1);
$str = "About us";
$str[0] = '';
substr_replace可以,或者绕绕远用正则表达式也可以。
其他相关推荐
其他相似问题5690人阅读
在php中字符替换函数有几个如有:str_replace、substr_replace、preg_replace、preg_split、str_split等函数,下面我来给大家总结介绍介绍。&
一、str_replace(find,replace,string,count)
作用:str_replace() 函数使用一个字符串替换字符串中的另一些字符。
find 必需。规定要查找的值。
replace 必需。规定替换 find 中的值的值。
string 必需。规定被搜索的字符串。
count 可选。一个变量,对替换数进行计数。
在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:
$arr = array(&blue&,&red&,&green&,&yellow&);
print_r(str_replace(&red&,&pink&,$arr,$i));
echo &Replacements: $i&;
[0] =& blue
[1] =& pink
[2] =& green
[3] =& yellow
Replacements: 1
补充:count如果被指定,它的值将被设置为替换发生的次数。
二、_replace(string,replacement,start,length)
作用:substr_replace() 函数把字符串的一部分替换为另一个字符串。
string 必需。规定要检查的字符串。
replacement 必需。规定要插入的字符串。
start 必需。规定在字符串的何处开始替换。
■正数 - 在第 start 个偏移量开始替换
■负数 - 在从字符串结尾的第 start 个偏移量开始替换
■0 - 在字符串中的第一个字符处开始替换
charlist 可选。规定要替换多少个字符。
■正数 - 被替换的字符串长度
■负数 - 从字符串末端开始的被替换字符数
■0 - 插入而非替换
echo substr_replace(&Hello world&,&earth&,6);
Hello earth
三、preg_replace ( pattern , replacement , subject,limit = -1 ,$count& )
作用:执行一个的搜索和替换
pattern 必需。需要搜索的模式。
replacement 必需。用于替换的字符串或数组。
subject 必需。需要替换的字符串或数组。
limit 替换的次数。-1为无限
count 完成替换的次数,变量
Example #1 使用后向引用紧跟数值原文
$string = 'April 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
&以上例程会输出:
April1,2003
Example #2 preg_replace()中使用基于索引的数组
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
以上例程会输出:
The bear black slow jumped over the lazy dog.
四、preg_split ( pattern , subject,limit = -1 ,flag& )
作用:通过分割字符串
pattern 必需。需要搜索的模式。
replacement 必需。用于替换的字符串或数组。
subject 必需。需要替换的字符串。
limit 被分割的字符串最多limit。
例 1672. preg_split() 例子:取得搜索字符串的成分
// split the phrase by any number of commas or space characters,
& &, r, t, n and f
$keys = preg_split (&/[s,]+/&, &hypertext language, programming&);
例 1673. 将字符串分割成字符
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
例 1674. 将字符串分割为匹配项及其偏移量
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
本例将输出:
[0] =& Array
[0] =& hypertext
[1] =& Array
[0] =& language
[2] =& Array
[0] =& programming
五、str_split ( subject,length)
作用:将字符串分割成数组
subject 字符串。
length 每一段的长度。
print_r(str_split(&Hello&));
print_r(str_split(&Hello&,3));
[0] =& Hel
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:184546次
积分:3454
积分:3454
排名:第9956名
原创:163篇
转载:67篇
评论:11条
(5)(3)(4)(5)(3)(1)(1)(1)(1)(1)(3)(26)(2)(25)(6)(10)(22)(11)(27)(19)(22)(15)(1)(1)(1)(1)(2)(1)(7)(3)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'前言:釜山行,暴露人性,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
今天说一个最简单的例子,就是在php中如何读取另一个html页面中的标签,并显示用户想输出的内容。
首先建立一个页面,命名为:test.html如下图所示:
&!doctype html&
&html lang="en"&
&meta charset="UTF-8"&
&title&模板&/title&
{$content}
需求:读取{$content}并输出用户想输出的内容。
其次建立另一个页面,mytest.php代码如下:
//给$content赋值,且是字符串的形式
$content = '我最近在读廖一梅的《像我这样笨拙的生活》';
//读取test.html文件中的所有内容,并以字符串的形式返回
$fileContent = file_get_contents('test.html');
//从$fileContent字符串中把{$content}替换为$content的内容,并返回字符串
$str = str_replace('{$content}',$content,$fileContent);
//最后输出内容
echo $str;
最后,通过页面 访问,可以访问到内容,结果如下:
总结:这其实只是php中最基本的一个例子,也算是一个思想吧,主要是为Smarty做铺垫。如有不妥,欢迎指正,谢谢!!
阅读(...) 评论()}

我要回帖

更多关于 php数组替换字符串 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信