Get Part of a String from a Long String in PHP

A few days ago, I faced a problem when I need to get part of a string in a long text. I feel felt very difficult to grab my required data. But then I tried to find the way and I finally did it. So now I am sharing it with you but first, let’s start the journey.

  • The Problem
  • Writing a Code using preg_match_all() and explode()
  • Code Explanation
  • Writing a Function Using strpos(), strlen() and substr()
  • Code Explanation
  • Finally, Get Part of a Tex from a Long String
  • Conclusion

The Problem

Let’s assume you have a long text and you need to extract a part of a string from that long text and you need to perform some action on that. For example, you need to save that specific text into the database or you want to send that data as an SMS message, etc. That was the problem that I faced a few days ago. The string/text that I had to work on it as is below:

SMSGatewayMe\Client\Model\MessageSearchResult Object ( [count:protected] => 8 [results:protected] => Array ( [0] => SMSGatewayMe\Client\Model\Message Object ( [id:protected] => 124840776 [deviceId:protected] => 119648 [message:protected] => You have received payment Tk 3,280.00 from 01684749437. Fee Tk 0.00. Balance Tk 4,800.00. TrxID 7HN8CFFJGQ at 23/08/2020 12:43 [status:protected] => received [log:protected] => Array ( [0] => SMSGatewayMe\Client\Model\MessageLog Object ( [status:protected] => received [occurredAt:protected] => DateTime Object ( [date] => 2020-08-23 06:49:23.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) [createdAt:protected] => DateTime Object ( [date] => 2020-08-23 06:49:23.000000 [timezone_type] => 1 [timezone] => +00:00 ) [updatedAt:protected] => DateTime Object ( [date] => 2020-08-23 06:49:23.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) [1] => SMSGatewayMe\Client\Model\Message Object ( [id:protected] => 124844159 [deviceId:protected] => 119648 [message:protected] => You have received payment Tk 3,150.00 from 01709655111. Fee Tk 0.00. Balance Tk 7,950.00. TrxID 7HN8CGAL1S at 23/08/2020 13:21 [status:protected] => received [log:protected] => Array ( [0] => SMSGatewayMe\Client\Model\MessageLog Object ( [status:protected] => received [occurredAt:protected] => DateTime Object ( [date] => 2020-08-23 07:22:13.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) [createdAt:protected] => DateTime Object ( [date] => 2020-08-23 07:22:13.000000 [timezone_type] => 1 [timezone] => +00:00 ) [updatedAt:protected] => DateTime Object ( [date] => 2020-08-23 07:22:13.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) [2] => SMSGatewayMe\Client\Model\Message Object ( [id:protected] => 124857335 [deviceId:protected] => 119648 [message:protected] => You have received payment Tk 2,430.00 from 01715732747. Fee Tk 0.00. Balance Tk 10,380.00. TrxID 7HN9CHUKAZ at 23/08/2020 14:48 [status:protected] => received [log:protected] => Array ( [0] => SMSGatewayMe\Client\Model\MessageLog Object ( [status:protected] => received [occurredAt:protected] => DateTime Object ( [date] => 2020-08-23 08:48:32.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) [createdAt:protected] => DateTime Object ( [date] => 2020-08-23 08:48:32.000000 [timezone_type] => 1 [timezone] => +00:00 ) [updatedAt:protected] => DateTime Object ( [date] => 2020-08-23 08:48:32.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) )

As you can see that it is a very long string and we’ll grab these kind of text information

You have received payment Tk 3,150.00 from 01709655111. Fee Tk 0.00. Balance Tk 7,950.00. TrxID 7HN8CGAL1S at 23/08/2020 13:21 

So you know the problem so let’s move to next step for solving the problem.

Writing a Code using preg_match_all() and explode()

OK, we’ll start from a simple text. You can use several methods to solve the problem. There are some PHP functions that will help us like substr(), strpos(), strlen(), preg_match_all(), explode(). Please note that we are not using all of these in one go. We’ll write two or three custom functions to solve the problem and these PHP functions can be used there. Let’s first start with a simple text:

plot {{105}} plot

From the above text, you need to extract “plot {{105}}”. You can use many PHP functions but here I am using preg_match_all() and explode() function to solve the problem. Here is the code. First, see it yourself then I’ll explain it.

<?php
$raw_string = "plot {{105}} plot";
$pattern = "/{{(.*?)}}/"; 
//will match everything in { }
if(preg_match_all($pattern,$raw_string,$matches)):
    $results = $matches[1];
	$get	=	 '{{'.$results[0].'}}';
	$saperate	=	explode($get,$raw_string);
	echo '<pre>';
	echo $saperate[0].$get;
else:
    echo 'No Matching';
endif;
?>

Code Explanation

Look at lines 2 and 3, I first declared two variables and saved values in it. First value “plot {{105}} plot” is a string and second “/{{(.*?)}}/” is our pattern for preg_match_all(). In the next few lines, I have used if condition with preg_match_all() and explode() function to do the job. At line 6 I am saving value of “$matches[1]” which is the array element of matched pattern and on line no 8 I explode the string to get my required string. Now I have an array in the “$saperate” variable and can be used as I want.

Now let’s move to another complicated string.

Writing a Function Using strpos(), strlen() and substr()

Take a look at below string

i love cute animals, like [animal]cat[/animal]!!!

and you need to extract the “cat” from that string. Now, look at the string format. The cat is enclosed in two more words “[animal]” and “[/animal]”. So we have to first these two words and then it will be easy to catch the “CAT”. This time we are going to write a Function to retrieve our desired string. Here is function

//Function
function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

//Usage
$string 		= 	"i love cute animals, like [animal]cat[/animal]!!!"; // String to be processed
$parsed = get_string_between($string, '[animal]', '[/animal]'); // Used function
echo $parsed; // Print Result

Code Explanation

In get_string_between() function we need 03 parameters. one is “$string” which is the full text\string. Next are “$start” and “$end” which are the words in between we’ll find our “cat”. We first used strpos() function to find the position of the start text and the used strlen() function to calculate the length of the text after that again used strpos() and finally substr() function subtracted the “cat” from the whole text data. Now let’s try our final example.

Finally, Get Part of a Tex from a Long String

In the beginning of this post there is a long text and I need only these types of lines from that text data

You have received payment Tk 3,150.00 from 01709655111. Fee Tk 0.00. Balance Tk 7,950.00. TrxID 7HN8CGAL1S at 23/08/2020 13:21 

You noticed in the full string that these types of lines are enclosed in “[message:protected]” and “[status:protected]” words. So our targets are these two words. In our previous example, there was a big problem that that code retrieves only one occurrence and I need every word or para that is in the stack. To solve this problem I have used explode() function alongwith LOOP strpos() and substr() functions. The code is very simple. Just explode the data then start loop and by using strpos() and substr() put all the required text into an array. After that, you will have all the strings that will exist in the whole text data. Code is here:

//Function for extracting required strings into an array
function tag_contents($string, $tag_open, $tag_close){
   foreach (explode($tag_open, $string) as $key => $value) {
       if(strpos($value, $tag_close) !== FALSE){
            $result[] = substr($value, 0, strpos($value, $tag_close));;
       }
   }
   return $result;
}

//Usage
$fullstring2   =   'SMSGatewayMe\Client\Model\MessageSearchResult Object ( [count:protected] => 8 [results:protected] => Array ( [0] => SMSGatewayMe\Client\Model\Message Object ( [id:protected] => 124840776 [deviceId:protected] => 119648 [message:protected] => You have received payment Tk 3,280.00 from 01684749437. Fee Tk 0.00. Balance Tk 4,800.00. TrxID 7HN8CFFJGQ at 23/08/2020 12:43 [status:protected] => received [log:protected] => Array ( [0] => SMSGatewayMe\Client\Model\MessageLog Object ( [status:protected] => received [occurredAt:protected] => DateTime Object ( [date] => 2020-08-23 06:49:23.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) [createdAt:protected] => DateTime Object ( [date] => 2020-08-23 06:49:23.000000 [timezone_type] => 1 [timezone] => +00:00 ) [updatedAt:protected] => DateTime Object ( [date] => 2020-08-23 06:49:23.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) [1] => SMSGatewayMe\Client\Model\Message Object ( [id:protected] => 124844159 [deviceId:protected] => 119648 [message:protected] => You have received payment Tk 3,150.00 from 01709655111. Fee Tk 0.00. Balance Tk 7,950.00. TrxID 7HN8CGAL1S at 23/08/2020 13:21 [status:protected] => received [log:protected] => Array ( [0] => SMSGatewayMe\Client\Model\MessageLog Object ( [status:protected] => received [occurredAt:protected] => DateTime Object ( [date] => 2020-08-23 07:22:13.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) [createdAt:protected] => DateTime Object ( [date] => 2020-08-23 07:22:13.000000 [timezone_type] => 1 [timezone] => +00:00 ) [updatedAt:protected] => DateTime Object ( [date] => 2020-08-23 07:22:13.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) [2] => SMSGatewayMe\Client\Model\Message Object ( [id:protected] => 124857335 [deviceId:protected] => 119648 [message:protected] => You have received payment Tk 2,430.00 from 01715732747. Fee Tk 0.00. Balance Tk 10,380.00. TrxID 7HN9CHUKAZ at 23/08/2020 14:48 [status:protected] => received [log:protected] => Array ( [0] => SMSGatewayMe\Client\Model\MessageLog Object ( [status:protected] => received [occurredAt:protected] => DateTime Object ( [date] => 2020-08-23 08:48:32.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) [createdAt:protected] => DateTime Object ( [date] => 2020-08-23 08:48:32.000000 [timezone_type] => 1 [timezone] => +00:00 ) [updatedAt:protected] => DateTime Object ( [date] => 2020-08-23 08:48:32.000000 [timezone_type] => 1 [timezone] => +00:00 ) ) ) )';
echo "<pre>";
print_r(tag_contents($fullstring2 , "[message:protected] =>" , "[status:protected]"));
echo "</pre>";

Conclusion

We discussed how to extract part of a string from a long text with a matching pattern. Next, we started with simple text and then we used some complicated strings. We also learnt about some PHP functions like substr(), strpos(), strlen(), preg_match_all(), explode(). Hope you enjoyed the tutorial. Here are some more tutorials of PHP. You can check it out.

Leave a Reply - I will show after approval