{"id":115,"date":"2019-05-24T16:45:09","date_gmt":"2019-05-24T16:45:09","guid":{"rendered":"https:\/\/howto.ciscoar.com\/?page_id=115"},"modified":"2019-08-17T03:43:19","modified_gmt":"2019-08-17T03:43:19","slug":"php-text-manipulation","status":"publish","type":"page","link":"https:\/\/lamp.ciscoar.com\/?page_id=115","title":{"rendered":"PHP TEXT MANIPULATION"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Combining text<\/h2>\n\n\n\n<p>You can easily combine two strings by using a technique called &#8220;concatenation&#8221;, in PHP you concatenate with the period &#8220;.&#8221; and to avoid getting as a result a new large string you must also include an space in between, that space is generated by using quotes , like in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">Full Name: &lt;?php echo $first_name.\" \".$last_name; ?><\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2019\/06\/php-combining-text-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2019\/06\/php-combining-text-1.jpg\" alt=\"\" class=\"wp-image-295\" width=\"726\" height=\"272\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2019\/06\/php-combining-text-1.jpg 726w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2019\/06\/php-combining-text-1-300x112.jpg 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Searching within text<\/h2>\n\n\n\n<p>The main reason why we search within text is to be able to fix typos or completing missing parts in the text that was entered by the users when filling up the online forms of our website.<\/p>\n\n\n\n<p>There is a useful function in PHP that can be used, it&#8217;s called <strong>strpos()<\/strong>, which stands for &#8220;string position&#8221;.<\/p>\n\n\n\n<blockquote style=\"text-align:center\" class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> <strong>strpos()<\/strong><\/p><p>Stands for &#8220;string position&#8221;<\/p><\/blockquote>\n\n\n\n<p>The following is an example on which we search for &#8220;facebook.com&#8221; inside the variable $facebook_url, if the function <strong>strpos()<\/strong> returns is FALSE, then we manually add the missing part and use the period to concatenate the whole URL.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$facebook_url = $_REQUEST['facebook_url'];\n$position = strpos($facebook_url, \"facebook.com\");\nif ($position === false) {\n  $facebook_url = \"http:\/\/www.facebook.com\/\" . $facebook_url;\n<\/code><\/pre>\n\n\n\n<p>Once we have the complete URL we can just put it using the HTML tag <strong>href<\/strong> as we usually do.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;a href=\"&lt;?php echo $facebook_url; ?>\">Your Facebook page&lt;\/a>&lt;br \/><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">String Position function possible outputs<\/h2>\n\n\n\n<p>As you can see, the <strong>strpos()<\/strong> function, can return two totally different things:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A number indicating position<\/li><li>The value FALSE<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Combining text You can easily combine two strings by using a technique called &#8220;concatenation&#8221;, in PHP you concatenate with the period &#8220;.&#8221; and to avoid getting as a result a new large string you must also include an space in between, that space is generated by using quotes , like in the following example: Searching &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/lamp.ciscoar.com\/?page_id=115\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP TEXT MANIPULATION&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-115","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":34,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages\/115\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages\/115\/revisions\/1409"}],"wp:attachment":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}