{"id":7294,"date":"2019-10-21T05:10:54","date_gmt":"2019-10-21T05:10:54","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7294"},"modified":"2021-11-16T05:45:13","modified_gmt":"2021-11-16T05:45:13","slug":"complete-reference-guide-of-php-strings","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/complete-reference-guide-of-php-strings\/","title":{"rendered":"Complete reference guide of PHP Strings!"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is String in PHP<\/h2>\n\n\n\n<p>A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation mark<\/p>\n\n\n\n<p> $my_string&nbsp;= &#8216;Hello World&#8217;; <\/p>\n\n\n\n<p>You can also use double quotation marks (&#8220;). However, single and double quotation marks work in different ways. Strings enclosed in single-quotes are treated almost literally, whereas the strings delimited by the double quotes replaces variables with the string representations of their values as well as specially interpreting certain escape sequences.<\/p>\n\n\n\n<p>The escape-sequence replacements are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>\\n<\/code>&nbsp;is replaced by the newline character<\/li><li><code>\\r<\/code>&nbsp;is replaced by the carriage-return character<\/li><li><code>\\t<\/code>&nbsp;is replaced by the tab character<\/li><li><code>\\$<\/code>&nbsp;is replaced by the dollar sign itself (<code>$<\/code>)<\/li><li><code>\\\"<\/code>&nbsp;is replaced by a single double-quote (<code>\"<\/code>)<\/li><li><code>\\\\<\/code>&nbsp;is replaced by a single backslash (<code>\\<\/code>)<\/li><\/ul>\n\n\n\n<p> &lt;?php <br>$my_str = &#8216;World&#8217;; echo &#8220;Hello, $my_str!&lt;br&gt;&#8221;;      \/\/<em> Displays: Hello World! <\/em>echo &#8216;Hello, $my_str!&lt;br&gt;&#8217;;      \/\/ <em>Displays: Hello, $my_str! <\/em><br>echo &#8216;&lt;pre&gt;Hello\\tWorld!&lt;\/pre&gt;&#8217;; \/\/ <em>Displays: Hello\\tWorld!<\/em><br>echo &#8220;&lt;pre&gt;Hello\\tWorld!&lt;\/pre&gt;&#8221;; \/\/ <em>Displays: Hello   World! echo &#8216;I\\&#8217;ll be <\/em>back&#8217;;            \/\/<em> Displays: I&#8217;ll be back <\/em><br>?&gt; <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Manipulating PHP Strings<\/h2>\n\n\n\n<p>PHP provides many built-in functions for manipulating strings like calculating the length of a string, find substrings or characters, replacing part of a string with different characters, take a string apart, and many others. Here are the examples of some of these functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Calculating the Length of a String<\/h2>\n\n\n\n<p>The&nbsp;<code>strlen()<\/code>&nbsp;function is used to calculate the number of characters inside a string. It also includes the blank spaces inside the string.<\/p>\n\n\n\n<p> &lt;?php $my_str = &#8216;Welcome to Tutorial Republic&#8217;;  <br> echo strlen($my_str); <br>?&gt;  <br> \/\/ Outputs: 28 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Counting Number of Words in a String<\/h2>\n\n\n\n<p>The&nbsp;<code>str_word_count()<\/code>&nbsp;function counts the number of words in a string.<\/p>\n\n\n\n<p> &lt;?php <br>$my_str = &#8216;The quick brown fox jumps over the lazy dog.&#8217;;   echo str_word_count($my_str);<br> ?&gt; <br> \/\/ Outputs: 9  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Replacing Text within Strings<\/h2>\n\n\n\n<p>The&nbsp;<code>str_replace()<\/code>&nbsp;replaces all occurrences of the search text within the target string.<\/p>\n\n\n\n<p> &lt;?php <br>$my_str = &#8216;If the facts do not fit the theory, change the facts.&#8217;;<br>echo str_replace(&#8220;facts&#8221;, &#8220;truth&#8221;, $my_str); <br> \/\/ Display replaced string <br>?&gt; <\/p>\n\n\n\n<p>The output of the above code will be:If the truth do not fit the theory, change the truth.<\/p>\n\n\n\n<p>You can optionally pass the fourth argument to the&nbsp;<code>str_replace()<\/code>&nbsp;function to know how many times the string replacements was performed, like this.<\/p>\n\n\n\n<p><br> &lt;?php <br>$my_str = &#8216;If the facts do not fit the theory, change the facts.&#8217;;   <br>\/\/ Perform string replacement<br>str_replace(&#8220;facts&#8221;, &#8220;truth&#8221;, $my_str, $count);   <br>\/\/ Display number of replacements performed <br>echo &#8220;The text was replaced $count times.&#8221;; <br>?&gt; <\/p>\n\n\n\n<p>The output of the above code will be:- The text was replaced 2 times. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reversing a String<\/h2>\n\n\n\n<p>The&nbsp;<code>strrev()<\/code>&nbsp;function reverses a string.<\/p>\n\n\n\n<p> &lt;?php <br>$my_str = &#8216;You can do anything, but not everything.&#8217;;   <br>echo strrev($my_str); <br>\/\/ Display reversed string  <br>?&gt; <\/p>\n\n\n\n<p>The output of the above code will be:-   <br>.gnihtyreve ton tub ,gnihtyna od nac uoY <\/p>\n\n\n\n<p><br><\/p>\n\n\n\n<p><br>?> <br><\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_62222\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_84271\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_62222\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is String in PHP A string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in single quotation mark $my_string&nbsp;= &#8216;Hello World&#8217;; You can also use double quotation marks (&#8220;). However, single&#8230;<\/p>\n","protected":false},"author":14,"featured_media":7343,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5431,177,5430,466],"class_list":["post-7294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-function","tag-php","tag-php-string","tag-string"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=7294"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7294\/revisions"}],"predecessor-version":[{"id":25374,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7294\/revisions\/25374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7343"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}