{"id":1060,"date":"2024-07-17T18:16:17","date_gmt":"2024-07-17T18:16:17","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1060"},"modified":"2024-07-17T18:46:28","modified_gmt":"2024-07-17T18:46:28","slug":"php-common-how-to-solutions","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/php-common-how-to-solutions\/","title":{"rendered":"PHP Common How to Solutions"},"content":{"rendered":"\n<p>In this blog section, i will guide you through common how to in PHP! let begin our journey with examples.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#replace-string-with-another\">How to replace the part of string with another string in php<\/a><\/li>\n\n\n\n<li><a href=\"#substrings_occur_in_a_string\">How to count substrings occurs in a string in php <\/a><\/li>\n\n\n\n<li>How to extract substring from a string in PHP<\/li>\n\n\n\n<li>How to Check If a String Contains a Specific Word in PHP<\/li>\n\n\n\n<li>How to count total words count in PHP<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"replace-string-with-another\">How to replace the part of a string with another string in PHP?<\/h2>\n\n\n\n<p>here we will guide you how to replace the part of a string with another string in PHP. For achieving this you can this php function <code>str_replace<\/code> to find and replace and portion of string with another string, In the following example of <strong><a href=\"https:\/\/appfinz.com\/website-development-company-in-delhi\">Best Web Development Company<\/a><\/strong> of the string <code>$my_str<\/code> is replaced by the string &#8220;Best Web Development Company Name&#8221;. Lets try this now and see how it works: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n$my_str = &quot;Appfinz Technologies&quot;;\n \n\/\/ Display replaced string\necho str_replace(&quot;Best Website Designing&quot;, &quot;Best Web Development Company&quot;, $my_str);\n?&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"substrings_occur_in_a_string\">How to count substrings occurs in a string in php?<\/h2>\n\n\n\n<p>You can use the PHP&nbsp;<code>substr_count()<\/code>&nbsp;function to find out how many times a given substring is appears or repeated inside a string.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n$quotes = &quot;If we are talking about website development, Appfinz technologies provides website development services in delhi, this is best website development company in delhi, if you want to make website then connect with us for website development&quot;;\n \necho substr_count($quotes, 'website'); \/\/ Outputs: 5\necho substr_count($quotes, 'website development'); \/\/ Outputs: 4\n?&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to extract substring from a string in PHP?<\/h2>\n\n\n\n<p>Using the PHP common function <code>substr()<\/code> we can achieve this, The PHP <code>substr()<\/code> is used to get the substring Therefore the part of a part of string from string can be extracted using this function. This function takes the start and length parameters to return the portion of string. <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n$str = &quot;Hello World!&quot;;\necho substr($str, 0, 5);  \/\/ Outputs: Hello\necho substr($str, 0, -7); \/\/ Outputs: Hello\necho substr($str, 0);     \/\/ Outputs: Hello World!\necho substr($str, -6, 5); \/\/ Outputs: World\necho substr($str, -6);    \/\/ Outputs: World!\necho substr($str, -12);   \/\/ Outputs: Hello World!\n?&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to Check If a String Contains a Specific Word in PHP?<\/h2>\n\n\n\n<p>we can use PHP strpos() function to check the specific words contains in another string or not. <\/p>\n\n\n\n<p>The&nbsp;<code>strpos()<\/code>&nbsp;function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns&nbsp;<code>false<\/code>. Also note that string positions start at 0, and not 1.<\/p>\n\n\n\n<p>Lets see this with a example: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n$word = &quot;website&quot;;\n$mystring = &quot;Appfinz Technologies is best website development company in india&quot;;\n \n\/\/ Test if string contains the word \nif(strpos($mystring, $word) !== false){\n    echo &quot;Word Found!&quot;;\n} else{\n    echo &quot;Word Not Found!&quot;;\n}\n?&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to count total words count in PHP?<\/h2>\n\n\n\n<p>We can do this by using php function called <code>str_word_count()<\/code>, Let see how we count total words in php. this function help you to count words very effectively.<\/p>\n\n\n\n<p>Note: You can also mix this function with javascript for real time words counts in textarea feilds. <\/p>\n\n\n\n<p>Let see with a example <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n$my_str = 'Appfinz Technologies owned by kishan kumar mishra';\n \n\/\/ Outputs: 7\necho str_word_count($my_str);\n?&gt;<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this blog section, i will guide you through common how to in PHP! let<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1060"}],"version-history":[{"count":8,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":1072,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1060\/revisions\/1072"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}