PHP : Extract part of a string starting from the middle
Extracting part of the string is a fairly common task in programming and sometimes it can be a bit challenging to extract string starting from a middle of a larger string.
To illustrate the problem here. Let say your :
Problem :
You have a string that looks like this :
expires Friday, December 23, 2014 @ 11:59pm
and you want to final string result to be like :
Friday, December 23, 2014
Solutions :
First solution :
Use substr(), strlen() and strpos() functions
$string = "expires Friday, December 23, 2014 @ 11:59pm";
$final = substr($string, 14, strpos($string, '@') - strlen($string));
Second solution :
Which is more efficient, but less simpler to grasp for those not familiar with regular expression
$string = "expires Friday, December 23, 2014 @ 11:59pm";
$final = preg_replace('/expires\s*(.*?)@/', '\1', $string);
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.1k Golang : Print instead of building pyramids
+4.9k Swift : Convert (cast) Float to Int or Int32 value
+22.5k Golang : Round float to precision example
+13.9k Javascript : Prompt confirmation before exit
+11.8k Golang : Find and draw contours with OpenCV example
+7.4k Golang : How to handle file size larger than available memory panic issue
+36k Golang : How to split or chunking a file to smaller pieces?
+8.1k Golang : Check if integer is power of four example
+4.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+30.7k error: trying to remove "yum", which is protected
+24.3k Golang : Change file read or write permission example
+13.5k Golang : Tutorial on loading GOB and PEM files