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
+5.7k How to let Facebook Login button redirect to a particular URL ?
+12k Golang : How to get a user home directory path?
+5.1k Javascript : Get operating system and browser information
+19.5k Golang : How to read float value from standard input ?
+10k How to test Facebook App on localhost ?
+7.3k Golang : On lambda, anonymous, inline functions and function literals
+31.1k Golang : Regular Expression for alphanumeric and underscore
+6.2k Golang : Process json data with Jason package
+3.6k Linux/MacOSX : Search and delete files by extension
+7.1k Golang : Number guessing game with user input verification example
+16.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+6k Golang : Squaring elements in array