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. leetcode 【 Two Sum 】python 实现

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  2. IOS开发学习笔记018- 一般控件的使用

    1.移动 2.动画 3.缩放 3.旋转 4.简化代码 5.总结 UIButton 的两种状态 normal highlighted  1.移动 OC语法规定:不允许直接修改某个对象中结构体属性的成员. ...

  3. Halcon17 Linux 下载

    Halcon17 Linux 下载地址:http://www.211xun.com/download_page_10.html HALCON 17 是一套机器视觉图像处理库,由一千多个算子以及底层的数 ...

  4. RESTful-rest_framework应用第二篇(get、post的序列化与反序列化)

    目的是: 利用rest_framework实现对数据库内容的查看get请求(序列化).提交保存数据post请求 (反序列化) rest_framework序列化组件(即查看和) 第一步:基于Djang ...

  5. X-UA-Compatible设置IE浏览器兼容模式

    文件兼容性用来告诉IE,让它如何来编译你的网页. 指定 文件兼容性模式 以下是指定为Emulate IE7 mode 兼容性范例. <html> <head> <!-- ...

  6. UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)

    题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...

  7. css中按钮的四种状态

    css中按钮有四种状态 1. 普通状态2. hover 鼠标悬停状态3. active 点击状态4. focus 取得焦点状态 .btn:focus{outline:0;} 可以去除按钮或a标签点击后 ...

  8. iterator & iterable

    一. java.lang.Iterable java.util.Iterator Iterator是迭代器类,而Iterable是接口. 好多类都实现了Iterable接口,这样对象就可以调用iter ...

  9. [AGC002D] Stamp Rally (并查集+整体二分)

    Description 给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的变的最大编号最小. Input ...

  10. linux之tee

    tee命令 tee把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中 如果想看到输出的同时,把输出也同时拷入一个文件,这个命令很合适 格式:tee -a file -a 表示文件追加到末尾 ...