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
+11.7k Golang : Get remaining text such as id or filename after last segment in URL path
+16.4k Golang : Set up source IP address before making HTTP request
+13.7k Javascript : Prompt confirmation before exit
+12.1k Golang : "https://" not allowed in import path
+30k Golang : How to redirect to new page with net/http?
+9.1k Facebook : Getting the friends list with PHP return JSON format
+11.2k Swift : Convert (cast) Float to String
+18.7k Golang : Populate dropdown with html/template example
+23.1k Find and replace a character in a string in Go
+14.6k Golang : package is not in GOROOT during compilation
+10.6k Golang : Natural string sorting example
+17.2k Golang : delete and modify XML file content