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.2k Golang : Close channel after ticker stopped example
+11.1k Golang : Forwarding a local port to a remote server example
+10.4k Golang : Find and draw contours with OpenCV example
+32.6k Golang : Proper way to set function argument default value
+5.2k PHP : Proper way to get UTF-8 character or string length
+7.3k Golang : Generate Datamatrix barcode
+11k Golang : calculate elapsed run time
+6k Golang : Squaring elements in array
+4.8k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+6.2k Golang : alternative to os.Exit() function
+8.9k Golang : Edge detection with Sobel method
+19.2k PHP : Convert(cast) int to double/float