PHP TEXT MANIPULATION

Combining text

You can easily combine two strings by using a technique called “concatenation”, in PHP you concatenate with the period “.” 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:

Full Name: <?php echo $first_name." ".$last_name; ?>

Searching within text

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.

There is a useful function in PHP that can be used, it’s called strpos(), which stands for “string position”.

strpos()

Stands for “string position”

The following is an example on which we search for “facebook.com” inside the variable $facebook_url, if the function strpos() returns is FALSE, then we manually add the missing part and use the period to concatenate the whole URL.

$facebook_url = $_REQUEST['facebook_url'];
$position = strpos($facebook_url, "facebook.com");
if ($position === false) {
  $facebook_url = "http://www.facebook.com/" . $facebook_url;

Once we have the complete URL we can just put it using the HTML tag href as we usually do.

<a href="<?php echo $facebook_url; ?>">Your Facebook page</a><br />

String Position function possible outputs

As you can see, the strpos() function, can return two totally different things:

  • A number indicating position
  • The value FALSE