{"id":15403,"date":"2020-06-25T14:19:01","date_gmt":"2020-06-25T14:19:01","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15403"},"modified":"2022-04-28T09:15:24","modified_gmt":"2022-04-28T09:15:24","slug":"how-to-creating-and-accessing-string-function-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-creating-and-accessing-string-function-in-php\/","title":{"rendered":"How to creating and accessing string function in PHP?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">What is String in PHP?<\/h4>\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 marks (&#8216;), like this:<\/p>\n\n\n\n<p><strong>Example:<\/strong> $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><strong>Here&#8217;s an example to clarify the differences between single and double quoted strings:<\/strong><\/p>\n\n\n\n<p>Example: <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/36fbed7bfd916f6f50f37c03a46f3069.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Manipulating PHP Strings<\/h3>\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<h3 class=\"wp-block-heading\">Calculating the Length of a String<\/h3>\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><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/2a693da8f3b39fdfe33c0936da1d30f8.js\"><\/script>\n\n\n\n<p><strong>outputs 24<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Counting Number of Words in a String<\/h3>\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><strong>Example<\/strong>:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/8c7a71efe0deed57956019cd7609ba3a.js\"><\/script>\n\n\n\n<p><strong>outputs 9<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Replacing Text within Strings<\/h3>\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><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/bf30914f6af61c58491d7fdc15d4136e.js\"><\/script>\n\n\n\n<p><strong>outputs <\/strong>:- If the truth do not fit the theory, change the truth.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Reversing a String<\/strong><\/h3>\n\n\n\n<p>The&nbsp;<code>strrev()<\/code>&nbsp;function reverses a string.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/f79936dd0984f2312fa06b074a3d7e59.js\"><\/script>\n\n\n\n<p><strong>outputs<\/strong>:- .gnihtyreve ton tub ,gnihtyna od nac uoY<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Strpos() &#8211; Search For a Text Within a String<\/strong><\/h3>\n\n\n\n<p>The PHP&nbsp;<code>strpos()<\/code>&nbsp;the function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/a0d53c286e9942da1c1ca17f06fcabfe.js\"><\/script>\n\n\n\n<p><strong>outputs<\/strong>:- 6<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_46305\"><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_36411\"  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_46305\"  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 marks (&#8216;), like this: Example: $my_string&nbsp;= &#8216;Hello World&#8217;; You can also use double quotation&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[6173,6172,6174,6175,6176],"class_list":["post-15403","post","type-post","status-publish","format-standard","hentry","category-php","tag-counting-number-of-words","tag-length","tag-replacing-text-within-strings","tag-reversing-a-string","tag-strpos-search-for-a-text-within-a-string"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15403","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15403"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15403\/revisions"}],"predecessor-version":[{"id":24716,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15403\/revisions\/24716"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}