I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.

我正在尋找一種方法,從字符串變量中提取前100個字符,並將其放入另一個變量中進行打印。

Is there a function that can do this easily?

有一個函數可以很容易地做到這一點嗎?

For example:

例如:

$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";

$string2 = 100charfunction($string1);

print $string2

To get:

得到:

I am looking for a way to pull the first 100 characters from a string vari

6 个解决方案

#1

151

$small = substr($big, 0, 100);

For String Manipulation here is a page with a lot of function that might help you in your future work.

對於字符串操作,這里有一個具有很多功能的頁面,這些功能可能對您以后的工作有所幫助。

#2

28

You could use substr, I guess:

你可以用substr,我猜

$string2 = substr($string1, 0, 100);

or mb_substr for multi-byte strings:

或多字節串的mb_substr:

$string2 = mb_substr($string1, 0, 100);

You could create a function wich uses this function and appends for instance '...' to indicate that it was shortened. (I guess there's allready a hundred similar replies when this is posted...)

你可以創建一個函數,它使用這個函數並附加例如'…表示它被縮短了。(我想當這篇文章發布的時候,已經有上百個類似的回復了…)

#3

19

$x = '1234567';

echo substr ($x, 0, 3); // outputs 123

echo substr ($x, 1, 1); // outputs 2

echo substr ($x, -2); // outputs 67

echo substr ($x, 1); // outputs 234567

echo substr ($x, -2, 1); // outputs 6

#4

18

try this function

試試這個功能

function summary($str, $limit=100, $strip = false) {

$str = ($strip == true)?strip_tags($str):$str;

if (strlen ($str) > $limit) {

$str = substr ($str, 0, $limit - 3);

return (substr ($str, 0, strrpos ($str, ' ')).'...');

}

return trim($str);

}

#5

15

A late but useful answer, PHP has a function specifically for this purpose.

PHP有一個專門用於此目的的函數,這是一個很晚但很有用的答案。

mb_strimwidth

$string = mb_strimwidth($string, 0, 100);

$string = mb_strimwidth($string, 0, 97, '...'); //optional characters for end

#6

2

Without php internal functions:

沒有php內部函數:

function charFunction($myStr, $limit=100) {

$result = "";

for ($i=0; $i

$result .= $myStr[$i];

}

return $result;

}

$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";

echo charFunction($string1);

php 提取前一百个字,如何在PHP中提取字符串的前100個字符?相关推荐

  1. vba中取小数点后两位数_如何在Excel中提取小数点后面的数字?

    Excel中,如果某个单元格中包含一个带小数,要用公式提取该数值小数点后面的数字,例如A1单元格中包含一个数值"59178.68",在B1单元格中输入下面的公式: =RIGHT(A ...

  2. linux如何提取文件名称,如何在Linux中提取路径和文件名的特定部分

    我目前的任务是将跨多个目录的大量文件重命名为不同的标识符.如何在Linux中提取路径和文件名的特定部分 所以我有几个目录,如:B01,B02,B03等,在每个目录的文件名如img01.23495.pn ...

  3. python字符串筛选输出_如何在Python中过滤字符串列表

    Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...

  4. 如何在JavaScript中获取字符串数组的字符串?

    本文翻译自:How do you get a string to a character array in JavaScript? How do you get a string to a chara ...

  5. python输入一串字符串、输出字符串中的数字_Python 字符串与数字输出方法 如何在python中输入字符串,输出十进制...

    python将字符串中的数字乘以2并输出整个字符串 2018 Amazon Jeff Bezos 1120 4036 Amazon Jeff Bezos 2240 def db(num): impor ...

  6. 如何在PHP中删除字符串中的所有空格? [重复]

    本文翻译自:How do I strip all spaces out of a string in PHP? [duplicate] Possible Duplicate: 可能重复: To str ...

  7. 如何在PowerShell中连接字符串和变量?

    本文翻译自:How do I concatenate strings and variables in PowerShell? Suppose I have the following snippet ...

  8. 如何在Python中拆分字符串

    使用字符串时,常见的操作之一是使用给定的分隔符将字符串拆分为子字符串数组.在本文中,我们将讨论如何在Python中拆分字符串. PYthon学习企鹅裙:88198-2657 领取python自动化编程 ...

  9. 字符串压缩 java_如何在Java中压缩字符串?

    如何在Java中压缩字符串? 我使用GZIPOutputStream或ZIPOutputStream压缩字符串(我的2222235278130938882小于20),但压缩结果比原始字符串长. 在某个 ...

最新文章

  1. DeepLearning tutorial(4)CNN卷积神经网络原理简介+代码详解
  2. gRPC客户端创建和调用原理解析
  3. 什么是 bootstrap 中的 break point
  4. Idea工具开发 SpringBoot整合JSP(毕设亲测可用)
  5. pytorch 和 tensorflow2.0 方法替换
  6. Linux服务器备份mySQL数据库_远程linux服务器mysql数据库定期备份和删除
  7. 看一次广告听30分钟歌 你愿意为QQ音乐新功能买单吗?
  8. 由过去的微软、苹果、IBM 看今天的阿里巴巴
  9. Android百分比布局支持和垂直TextView
  10. Windows下安装和配置Java JDK
  11. 【图像隐写】基于matlab高斯模型JPEG图像隐写【含Matlab源码 367期】
  12. 数据挖掘概念与技术_第三版_课后习题
  13. 详细通用phpstudy本地搭建网站以及cms搭建教程
  14. 在element框架中使用videojs-markers插件时,无法正常引入的坑
  15. 最新鸽哒IM即时通讯系统源码+带安装教程
  16. 路由器有以下几种联网接口
  17. html5 red5,一个基于red5+flash的网络聊天室red5Chat
  18. 免费在线文档转换工具,一招告诉你超简单.
  19. 《牛津字典精华总结》- 初阶系列 - 字母 - B
  20. 【Electron】使用vue-electron+electron-store创建项目,NSIS打包为exe可安装文件

热门文章

  1. 比较创建几种线程的方式
  2. eclipse 安装git
  3. 计算机网络协议包头赏析-TCP
  4. 上海音乐学院计算机考研难吗,上海音乐学院考研难吗?一般要什么水平才可以进入?...
  5. springboot指定首页(静态资源导入)
  6. jsp页面路径问题(404)
  7. 分享几个可供学习,休闲的网站
  8. Linux编译dhcpd,linux中搭建dhcpd服务器
  9. 【转帖】windows命令行中java和javac、javap使用详解(java编译命令)
  10. linux日常常用命令分析