php字符串实例
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 , 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()
$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字符串实例的更多相关文章
- 4. python 修改字符串实例总结
4. python 修改字符串实例总结 我们知道python里面字符串是不可原处直接修改的,为了是原来的字符串修改过来,我们有一下方法: 1.分片和合并 >>> a='abcde' ...
- dom4j解析xml字符串实例
DOM4J 与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml ...
- c语言字符串实例
例子:涉及字符串.字符.指针.++等 例一:字符串与字符 #include <stdio.h> void reverse(char *str) { char *end=str; print ...
- python 基本数据类型--字符串实例详解
字符串(str) :把字符连成串. 在python中⽤', ", ''', """引起来的内容被称为字符串 . 注意:python中没有单一字符说法,统一称叫字 ...
- js替换数组中字符串实例
这个是替换数组中的一个对象字符串: 直接上代码: var aaa=[ {"name":"张珊","sex":"man"} ...
- python 字符串实例:检查并判断密码字符串的安全强度
检查并判断密码字符串的安全强度 import string def check(pwd): #密码必须至少包含六个字符 if not isinstance(pwd,str) or len(pwd)&l ...
- shell 截取字符串实例教程
本节内容:shell字符串截取方法 1,去掉字符串最左边的字符 [root@jbxue ~]$ vi test.sh 1 STR="abcd" 2 STR=${STR#" ...
- C中的字符串实例
1.#include <stdio.h>#include <assert.h>size_t strlen(const char* s){ return ( assert( ...
- C#_StringBuilder分离字符串实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stri ...
随机推荐
- python学习-- 在django中,执行原始sql语句
from django.shortcuts import render, redirect from news.models import Article, Column def test(reque ...
- MongoDB01——安装MangoDB
一.MongoDB的下载 到MongoDB的官网——https://www.mongodb.com/download-center/community,选择要下载的版本,点击Download 二.安装 ...
- Swift全栈开发
前段时间学习了一下Swift web framework-Vapor, 类似于PHP Laravel的web框架. Apple也成立了Server APIs Project, Server-side ...
- iOS中常见的自定义宏
//字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str leng ...
- puppet实战之master-agent
author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- master作为puppet模块的管理者,通过配置各agent节点的配置文件,使age ...
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- 【距离GDOI:141天】 滚入数位DP的坑
作为博客园的第一篇...我都不知道要写什么了 ... 其实今天很没状态,就当吐槽吧... 嗯,被黄神带去写treap+可持久化线段树,然后在可持久化的删除上面跪了两天,真的是一跪不起.我已经连续多久没 ...
- webstorm的stylus编译环境搭建
http://www.cnblogs.com/pizitai/p/6186513.html
- 急速安装Ubuntu/windows双操作系统
本文出自:http://www.cnblogs.com/svitter FAQ 因为很多人都不看FAQ,比如像我,所以直接把FAQ写在最前面,然后把正文卸载最后面逼你看- - 常用软件下载(官网) d ...
- poj 2836 Rectangular Covering
Rectangular Covering Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2776 Accepted: 7 ...