1. 首页
  2. php

PHP – 字符串

它们是字符序列,例如“PHP支持字符串操作”。

 – 函数参考PHP字符串函数中给出了内置字符串函数

PHP - 字符串

以下是字符串的有效示例

$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

单引号字符串几乎按字面处理,而双引号字符串用其值替换变量以及特别解释某些字符序列。

<?php
   $variable = "name";
   $literally = 'My $variable will not print!\\n';
   
   print($literally);
   print "<br />";
   
   $literally = "My $variable will print!\\n";
   
   print($literally);
?>

这将产生以下结果 –

My $variable will not print!\n
My name will print!\n

字符串长度没有人为限制 – 在可用内存的范围内,你应该能够制作任意长的字符串。

由双引号分隔的字符串(如“this”中所示)由PHP以下两种方式进行预处理 –

  • 以反斜杠(\)开头的某些字符序列将替换为特殊字符
  • 变量名(以$开头)将替换为其值的字符串表示形式。

逃逸序列替换是 –

  • \ n由换行符替换
  • \ r \ n由回车符替换
  • \ t由制表符替换
  • \ $被美元符号本身取代($)
  • \“被一个双引号(”)取代
  • \\由单个反斜杠(\)替换

字符串连接运算符

要将两个字符串变量连接在一起,请使用点(。)运算符 –

<?php
   $string1="Hello World";
   $string2="1234";
   
   echo $string1 . " " . $string2;
?>

这将产生以下结果 –

Hello World 1234

如果我们查看上面的代码,您会看到我们使用了连接运算符两次。这是因为我们必须插入第三个字符串。

在两个字符串变量之间,我们添加了一个带有单个字符的字符串,一个空格,以分隔这两个变量。

使用strlen()函数

strlen()函数用于查找字符串的长度。

让我们找到字符串“Hello world!”的长度。 –

<?php
   echo strlen("Hello world!");
?>

这将产生以下结果 –

12

当知道字符串何时结束很重要时,字符串的长度通常用在循环或其他函数中。(即在循环中,我们希望在字符串中的最后一个字符后停止循环)

使用strpos()函数

strpos()函数用于搜索字符串中的字符串或字符。

如果在字符串中找到匹配项,则此函数将返回第一个匹配项的位置。如果未找到匹配项,则返回FALSE。

让我们看看我们是否可以在字符串中找到字符串“world” –

<?php
   echo strpos("Hello world!","world");
?>

这将产生以下结果 –

 6

如您所见,字符串“world”在我们的字符串中的位置是位置6.它是6而不是7的原因是字符串中的第一个位置是0,而不是1。

原创文章,作者:零久SEO,如若转载,请注明出处:http://www.seo0j.com/320.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论列表(16条)

  • 100% real jordans for cheap 2020年2月23日 上午8:40

    Thanks a lot for providing individuals with remarkably wonderful opportunity to read in detail from this blog. It is often very pleasant plus jam-packed with amusement for me personally and my office friends to search your web site more than 3 times a week to see the fresh items you will have. Not to mention, we’re at all times contented with your magnificent thoughts you give. Selected 4 tips on this page are in truth the best I have had.

  • moncler jackets 2020年2月26日 上午4:39

    I simply desired to say thanks once again. I am not sure what I could possibly have undertaken without the entire techniques provided by you regarding my field. It absolutely was a very traumatic situation for me personally, but seeing your expert tactic you solved it made me to weep with joy. I am grateful for this support and as well , have high hopes you find out what an amazing job you were doing instructing many people via a blog. Most probably you have never met all of us.

  • yeezy 2020年2月28日 下午6:18

    I just wanted to type a simple note in order to thank you for the fabulous tips you are showing here. My time intensive internet lookup has now been rewarded with reputable information to talk about with my best friends. I ‘d believe that we visitors are really blessed to exist in a superb place with so many outstanding people with insightful principles. I feel rather fortunate to have used your entire web page and look forward to tons of more entertaining moments reading here. Thanks again for everything.

  • nike lebron 15 2020年3月2日 上午7:14

    My wife and i were now joyous Chris managed to complete his studies from your precious recommendations he grabbed through the site. It is now and again perplexing to simply continually be handing out concepts which often many others may have been trying to sell. Therefore we realize we have the website owner to give thanks to because of that. All of the illustrations you’ve made, the straightforward blog menu, the relationships you will aid to promote – it’s got all impressive, and it is assisting our son and us know that the matter is fun, and that is exceedingly pressing. Thanks for all the pieces!

  • yeezy shoes 2020年3月5日 上午3:34

    I want to show my gratitude for your generosity in support of all those that should have assistance with your topic. Your very own dedication to passing the message all through was surprisingly powerful and has always encouraged most people much like me to achieve their ambitions. Your amazing interesting advice implies a great deal a person like me and far more to my office workers. Thanks a ton; from each one of us.

  • jordan retro 2020年3月7日 上午9:41

    I definitely wanted to send a small note in order to say thanks to you for those unique guides you are writing on this website. My time-consuming internet research has now been paid with wonderful knowledge to talk about with my family. I ‘d assume that many of us readers are definitely lucky to be in a useful website with many perfect people with insightful points. I feel pretty blessed to have discovered the webpages and look forward to so many more pleasurable moments reading here. Thanks a lot once again for a lot of things.

  • kobe sneakers 2020年3月10日 上午4:50

    A lot of thanks for your entire work on this web site. Debby enjoys setting aside time for research and it’s easy to understand why. A lot of people notice all about the powerful form you create practical techniques through the web blog and even boost contribution from visitors about this area then our favorite girl is certainly being taught a whole lot. Take advantage of the rest of the new year. You have been conducting a superb job.

  • supreme clothing 2020年3月13日 上午10:51

    Thanks a lot for providing individuals with an extraordinarily pleasant opportunity to read critical reviews from this website. It’s usually so amazing and also stuffed with a good time for me and my office friends to search your website not less than 3 times in 7 days to learn the newest stuff you have got. And definitely, I am always fascinated considering the breathtaking guidelines you serve. Certain 2 points in this posting are clearly the very best we have had.

  • adidas stan smith shoes 2020年3月16日 上午9:05

    I in addition to my guys were found to be going through the best strategies from the website and so suddenly I got an awful suspicion I had not thanked you for those strategies. These people ended up as a consequence stimulated to study them and have truly been making the most of these things. I appreciate you for really being quite helpful and then for using such outstanding themes most people are really desirous to understand about. Our honest apologies for not expressing appreciation to you earlier.

  • off white nike 2020年3月19日 上午4:47

    I in addition to my buddies were actually checking out the best secrets located on your website and so the sudden I had a horrible feeling I had not thanked the web blog owner for those tips. Most of the young boys were consequently excited to learn them and have now sincerely been loving them. I appreciate you for getting indeed accommodating and for settling on these kinds of beneficial ideas millions of individuals are really needing to know about. Our own sincere apologies for not saying thanks to sooner.

  • cheap jordans 2020年3月22日 上午2:39

    I would like to convey my love for your kindness giving support to men and women who really want assistance with this subject. Your very own dedication to passing the solution along has been extremely practical and have specifically helped those much like me to arrive at their goals. Your own interesting guide signifies a whole lot a person like me and somewhat more to my office colleagues. Thank you; from everyone of us.

  • nike vapormax 2020年3月24日 上午11:58

    I wish to express appreciation to the writer for bailing me out of this dilemma. Right after exploring through the world wide web and meeting recommendations which are not productive, I was thinking my entire life was well over. Living devoid of the answers to the problems you have resolved through this website is a critical case, as well as the ones which could have in a wrong way damaged my entire career if I had not discovered your blog post. Your primary expertise and kindness in taking care of the whole lot was crucial. I don’t know what I would’ve done if I had not discovered such a stuff like this. I am able to now look ahead to my future. Thanks very much for your skilled and amazing help. I won’t be reluctant to recommend your web page to any person who should have support on this problem.

  • Free Inpatient Drug Rehab Centers Near Me 2020年3月26日 下午12:48

    Substance Abuse Programs http://aaa-rehab.com Drug Rehab Centers http://aaa-rehab.com Substance Abuse Clinic
    http://aaa-rehab.com

  • michael kors 2020年3月27日 上午5:29

    I together with my friends were found to be digesting the excellent things from your site then quickly developed a horrible feeling I never thanked the website owner for those secrets. These young boys came very interested to read through them and have very much been making the most of these things. We appreciate you actually being very considerate and also for using these kinds of fantastic tips millions of individuals are really wanting to discover. My very own sincere apologies for not saying thanks to sooner.

  • jordan retro 2020年3月30日 下午2:03

    My spouse and i have been quite happy Ervin managed to carry out his research with the ideas he was given from your own web pages. It is now and again perplexing to just choose to be giving away secrets and techniques that most people have been selling. Therefore we see we need the website owner to give thanks to for this. All of the explanations you have made, the easy site navigation, the friendships your site aid to create – it’s got many incredible, and it’s really assisting our son and us know that the article is awesome, and that’s especially essential. Many thanks for all the pieces!

  • supreme clothing 2020年4月2日 下午12:32

    I intended to post you the tiny word in order to say thanks once again about the extraordinary suggestions you have shared here. It’s certainly incredibly open-handed with people like you to grant without restraint exactly what a number of people could have sold for an e-book to earn some profit on their own, notably seeing that you could have done it in case you desired. These strategies as well acted to become a fantastic way to comprehend someone else have a similar zeal like mine to grasp much more pertaining to this problem. I am sure there are many more pleasant instances in the future for individuals who view your blog post.

联系我们

15555333984

在线咨询:点击这里给我发消息

邮件:lxa6@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code