服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php字符串按照单词进行反转的方法

php字符串按照单词进行反转的方法

2020-09-10 15:57work24 PHP教程

这篇文章主要介绍了php字符串按照单词进行反转的方法,涉及php中array_reverse、explode及implode等函数的使用技巧,需要的朋友可以参考下

本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:

下面的php代码可以将字符串按照单词进行反转输出,实际上市现将字符串按照空格分隔到数组,然后对数组进行反转输出

?
1
2
3
4
5
6
7
8
9
10
<?php
$s = "Reversing a string by word";
// break the string up into words
$words = explode(' ',$s);
// reverse the array of words
$words = array_reverse($words);
// rebuild the string
$s = implode(' ',$words);
print $s;
?>

输出结果如下:
word by string a Reversing

希望本文所述对大家的php程序设计有所帮助。

延伸 · 阅读

精彩推荐