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
+13.4k Golang : Send email with attachment(RFC2822) using Gmail API example
+6.2k Golang : Get environment variable
+3.9k JavaScript : Rounding number to decimal formats to display currency
+4.3k Golang : micron to centimeter example
+6k How to let Facebook Login button redirect to a particular URL ?
+16.5k Golang : Clone with pointer and modify value
+13.1k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+33.5k Golang : Integer is between a range
+5.9k Golang : Warp text string by number of characters or runes example
+6.9k Gogland : Where to put source code files in package directory for rookie
+3.9k JavaScript: Add marker function on Google Map
+11.8k Golang : Transform comma separated string to slice example