2.双引号字符串

<?php
print "I have gone to the store.";
print "The sauce cost \$10.25.";
$cost= '$10.25';
print "The sauce cost $cost.";
print "The sauce cost \$\061\060.\x32\x35."; ?>

3.用strpos()来查找子字符串

<?php

$e_mail='abc@sina.com';
if(strpos($e_mail,'@')===false)
{
print 'There was no @ in the e-mail address!';
}
else {
print 'There was @ in the e-mail address';
} ?>

相等符要用===  ,不等符要用!==  ,因为如果要找的字符串在开始处,那么会返回0,0和false相等。

4.提取子字符串substr()

string substr ( string $string , int $start [, int $length ] )
 
<?php

print substr('watch out for that tree',6,5);

?>

如果$start 的值大于字符串的长度,substr()返回false

如果$start加$length超过了字符串的结尾,substr()返回从位置$start开始至字符串结尾的所有字符

如果$start是负值,substr()会从这个字符串的结尾处开始反向推算,来确定要返回的子字符串的开始位置

当一个负的$start值超过了这个字符串的开始位置时(例如,如果对于长度为20的字符串设置的$-27),substr()将$start的值视为0

如果$length是负值,substr()会从这个字符串的结尾处反向推算,来确定要返回的子字符串的结尾位置(也就是从结尾处去掉length的绝对值个字符)

5.替换子字符串substr_replace()

mixed substr_replace ( mixed $string , string $replacement , int $start [, int $length ] )
<?php

print substr_replace('My pet is a blue dog', 'fish', 12);
print substr_replace('My pet is a blue dog', 'green', 12,4);
$credit_card='4111 1111 1111 1111';
print substr_replace($credit_card, 'xxxx ', 0, strlen(($credit_card)-4)); ?>

结果

My pet is a fish
My pet is a green dog
xxxx 1111 1111 1111

6按字反转字符串

<?php

$s="Once upon a time there was a turtle.";
//将字符串分解为独立的字
$words=explode(' ',$s);
//反转这个字数组
$words=array_reverse($words);
//重建反转后的字符串
$s= implode(' ', $words);
print $s; ?>

可简化的写成

$reversed_s=  implode(' ', array_reverse(explode(' ', $s)));

运行结果

turtle. a was there time a upon Once

php字符串实例的更多相关文章

  1. 4. python 修改字符串实例总结

    4. python 修改字符串实例总结 我们知道python里面字符串是不可原处直接修改的,为了是原来的字符串修改过来,我们有一下方法: 1.分片和合并 >>> a='abcde'  ...

  2. dom4j解析xml字符串实例

    DOM4J 与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml ...

  3. c语言字符串实例

    例子:涉及字符串.字符.指针.++等 例一:字符串与字符 #include <stdio.h> void reverse(char *str) { char *end=str; print ...

  4. python 基本数据类型--字符串实例详解

    字符串(str) :把字符连成串. 在python中⽤', ", ''', """引起来的内容被称为字符串 . 注意:python中没有单一字符说法,统一称叫字 ...

  5. js替换数组中字符串实例

    这个是替换数组中的一个对象字符串: 直接上代码: var aaa=[ {"name":"张珊","sex":"man"} ...

  6. python 字符串实例:检查并判断密码字符串的安全强度

    检查并判断密码字符串的安全强度 import string def check(pwd): #密码必须至少包含六个字符 if not isinstance(pwd,str) or len(pwd)&l ...

  7. shell 截取字符串实例教程

    本节内容:shell字符串截取方法 1,去掉字符串最左边的字符 [root@jbxue ~]$ vi test.sh 1 STR="abcd" 2 STR=${STR#" ...

  8. C中的字符串实例

    1.#include <stdio.h>#include <assert.h>size_t strlen(const char* s){    return ( assert( ...

  9. C#_StringBuilder分离字符串实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stri ...

随机推荐

  1. ogre3D学习基础3 -- 粒子与表层脚本

    9.粒子脚本 粒子脚本允许你实例化地在你的脚本代码中定义粒子系统,而不必在源代码中进行设置,使得你做任何修改都能得到快速回应.脚本里定义的粒子系统被用作模板,并且多个实际的系统可以在运行时从这里被创建 ...

  2. [python工具][pycharm]pycharm licence activation失效的解决方法

    推荐网站: http://idea.lanyus.com/ 1 请将“0.0.0.0 account.jetbrains.com”添加到C:\Windows\System32\Drivers\etc\ ...

  3. 文本处理grep命令

    this is a words file. words words to be , , , , , , , , , , beginning linux programming 4th edition ...

  4. C#知识点<2>

    1. ? : 运算符(真2假3) 我们已经在前面的章节中讲解了 条件运算符 ? :,可以用来替代 if...else 语句.它的一般形式如下: Exp1 ? Exp2 : Exp3; 其中,Exp1. ...

  5. ssh-add Could not open a connection to your authentication agent.

    ssh-add 报错Could not open a connection to your authentication agent. 需要执行以下代码 eval `ssh-agent -s` ssh ...

  6. [SDOI2011][bzoj2245] 工作分配 [费用流]

    题面 传送门 思路 数据范围n,m<=250 分配任务问题 这是典型的"看到数据范围就知道算法"类型 而且我们发现我们要保证一定产出的情况下最小化花费 这句话等价于保证一定流 ...

  7. webRTC前世今生

    WebRTC 的前世今生 本文由 rwebrtc 翻译 WebRTC 技术是激烈的开放的 Web 战争中一大突破.-Brendan Eich, inventor of JavaScript 无插件实时 ...

  8. set和dict

    dict属于mapping类型 from collections.abc import Mapping,MutableMapping from collections.abc import __all ...

  9. [六省联考2017]组合数问题 (矩阵优化$dp$)

    题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...

  10. CSS3 基本属性 浅析(含选择器、背景阴影、3D转换、动画等)

    1渐进增强原则 2私有前缀  不同浏览器在发布不同版本(一般测试版)时会加前缀,新增属性加上前缀进行支持测试:     Chrome浏览器:-webkit-border-radius: 5px;   ...