PHP substring extraction. Get the string before the first '/' or the whole string
Use explode()
$arr = explode("/", $string, 2);
$first = $arr[0];
In this case, I'm using the limit
parameter to explode
so that php won't scan the string any more than what's needed.
Return the portion of a string before the first occurrence of a character in PHP
You could do this:
$string = 'The quick brown fox jumped over the lazy dog';
$substring = substr($string, 0, strpos($string, ' '));
But I like this better:
list($firstWord) = explode(' ', $string);
PHP get everything in a string before underscore
You should simple use:
$imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_"));
I don't see any reason to use explode
and create extra array just to get first element.
You can also use (in PHP 5.3+):
$imagePreFix = strstr($fileinfo['basename'], '_', true);
php string function to get substring before the last occurrence of a character
This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:
$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);
How to get everything after a certain character?
The strpos()
finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.
$data = "123_String";
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;
If you also want to check if the underscore character (_
) exists in your string before trying to get it, you can use the following:
if (($pos = strpos($data, "_")) !== FALSE) {
$whatIWant = substr($data, $pos+1);
}
Using PHP to Get Character Before the First Period?
you can use strpos and substr functions
strpos - it'll return index of first occurrence of substring
substr - return specific part of string
$domain='example.com';
$lastchar=substr($domain, strpos($domain,'.')-1, 1);
How to get a substring between two strings in PHP?
If the strings are different (ie: [foo] & [/foo]), take a look at this post from Justin Cook.
I copy his code below:
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = 'this is my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');
echo $parsed; // (result = dog)
Remove portion of a string after a certain character
$variable = substr($variable, 0, strpos($variable, "By"));
In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.
how to get everything before and after a certain word in string
If you are certain that your input string contains " times "
then you can do this:
$input = "5 times 8";
list($a, $b) = explode(' times ', $input);
echo $a; // "5"
echo $b; // "8"
If you need to do something a little more complex, use regular expressions:
$input = "5 times 8";
if (preg_match('!(\d+)\s*times\s*(\d+)!i', $input, $m)){
$a = $m[1];
$b = $m[2];
}
get string before last occurrence of specific character
echo dirname('js/jstree/_lib/jquery.js');
Related Topics
Prevent Sent Emails Treated as Junk Mails Using PHP Mail Function
Adding Months to a Date in JavaScript
How to Strip a Tag and All of Its Inner HTML Using the Tag'S Id
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
How to Use a String as an Array Index Path to Retrieve a Value
How to Split a String by Multiple Delimiters in PHP
If Statement in the Middle of Concatenation
PHP 5 Strpos() Difference Between Returning 0 and False
Upload Multiple Files With PHP and Jquery
Restructure Multidimensional Array of Column Data into Multidimensional Array of Row Data
Switch PHP Versions on Commandline Ubuntu 16.04
How to Check If a String Starts With a Specified String
How to Clear Apc Cache Entries
Fastest Way to Convert String to Integer in PHP