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
+7.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+11.4k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+7.3k Useful methods to access blocked websites
+14.1k Golang : How to login and logout with JWT example
+28k Golang : missing Git command
+4.8k Fix fatal error: evacuation not done in time problem
+6.3k Android Studio : AlertDialog to get user attention example
+14k Golang : Get timezone offset from date or timestamp
+6.8k Javascript : How to check a browser's Do Not Track status?
+14.6k Golang : Update database with GORM example
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?