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
+21.9k Golang : Convert uint value to string type
+10.8k Golang : Compress and decompress file with compress/flate example
+6.5k Swift : Convert (cast) Character to Integer?
+5.9k Gogland : Single File versus Go Application Run Configurations
+3.6k Python : Create Whois client or function example
+5.2k Golang : Transform lisp or spinal case to Pascal case example
+4.3k Get website traffic ranking with Similar Web or Alexa
+6.5k Golang : Convert word to its plural form example
+17.4k Mac OSX : Homebrew and Golang
+8k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+8.9k Golang : Create S3 bucket with official aws-sdk-go package