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
+14.3k Golang : How to get Unix file descriptor for console and file
+9.2k Golang : Channels and buffered channels examples
+10.7k Golang : Change date format to yyyy-mm-dd
+5k Cash Flow : 50 days to pay your credit card debt
+6.7k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+16.7k How to enable MariaDB/MySQL logs ?
+4.4k Swift : Convert (cast) Float to Int or Int32 value
+23.2k Golang : Generate MD5 checksum of a file
+5.6k CodeIgniter : form input set_value cause " to become & quot
+2.3k Golang : Fix go-cron set time not working issue
+9.1k Golang : Get current, epoch time and display by year, month and day