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
+18.6k Golang : How to make function callback or pass value from function as parameter?
+20.8k Golang : How to force compile or remove object files first before rebuild?
+9.9k Golang : Print how to use flag for your application example
+6.9k Golang : Transform lisp or spinal case to Pascal case example
+4.8k Python : Convert(cast) bytes to string example
+31.4k Golang : Convert an image file to []byte
+7.2k Golang : Check to see if *File is a file or directory
+14k Golang : Get uploaded file name or access uploaded files
+13.5k Golang : Gin framework accept query string by post request example
+19.5k Golang : Append content to a file
+19.4k Golang : Set or Add HTTP Request Headers
+13.3k Golang : Query string with space symbol %20 in between