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. RSA进阶之两个N的公约数

    适用场景: 给你两个n,n1和n2. 两个数都很大,不好分解. 但是这数刚好有质数公因子(试试欧几里得辗转相除跑完之后,就是不断地相除就可以了,4000多位也是很快的),那不就相当于间接的分解出q或者 ...

  2. Python + Selenium 自动化环境搭建过程

    1.  所需组建 1.1  Selenium for python 1.2  Python 1.3  Notepad++ 作为刚初学者,这里不建议使用Python IDE工具,选择一个功能强大的记事本 ...

  3. day02_05.除数与被除数

    第5题 除数与被除数 编程需要一定数学能力,在这看看你找到了几个有用条件, 又该如何来运用他们呢? 学习是互通的 题目:两个自然数相除,商3余10,被除数,除数,商,余数的和是163,求被除数,除数. ...

  4. sql cte的使用

    cte是可以连续使用的,多个cte用逗号隔开,但是只能有一个with 百度文章标题:Sql server中使用with as 提高性能+高效分页

  5. linux 相关知识

    在mac 终端中可以直接访问ssh 命令:ssh root@127.0.0.*  批量删除文件: 当前目录下所有 *.html文件,除了index.html             [root@whr ...

  6. Callable、Future、FutureTask浅析

    1.Callable<V>接口 Runnable接口 public interface Runnable { public abstract void run(); } Callable ...

  7. BZOJ 4826 [Hnoi2017]影魔 ——扫描线 单调栈

    首先用单调栈和扫描线处理出每一个数左面最近的比他大的数在$l[i]$,右面最近的比他大的数$r[i]$. 然后就可以考虑每种贡献是在什么时候产生的. 1.$(l[i],r[i])$产生$p1$的贡献 ...

  8. mrpt安装

    1.mrpt2.0参 See PPA for mrpt 2.0 branch (for mrpt 1.5.* read here). sudo add-apt-repository ppa:josel ...

  9. Codeforces Round #362 (Div. 2) A 水也挂

    A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. 沼泽鳄鱼(bzoj 1898)

    Description 潘塔纳尔沼泽地号称世界上最大的一块湿地,它地位于巴西中部马托格罗索州的南部地区.每当雨季来临,这里碧波荡漾.生机盎然,引来不少游客.为了让游玩更有情趣,人们在池塘的中央建设了几 ...