Hello,
I have a field that is in this format
5551112391^HUMAN^HUMAN-800-800^6-main^^
The "5551112391" before the ^ is the number. How would I grab only that and not any of that other data?
Would you do this with regex? Let me know!
Thanks.
Hello,
I have a field that is in this format
5551112391^HUMAN^HUMAN-800-800^6-main^^
The "5551112391" before the ^ is the number. How would I grab only that and not any of that other data?
Would you do this with regex? Let me know!
Thanks.
You can make use of explode:
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$arr = explode('^',$var);
$num = $arr[0];
Using regex:
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
if(preg_match('/^(\d+)/',trim($var),$m)){
$num = $m[1];
}
You're doing it in completely wrong way.
You treat mysql database as a flat text file. But it is not.
To get only certain data from the table, you should not select all rows and then compare one by one but make database do it for you:
SELECT * FROM table WHERE number=5551112391
Regex overkill, nice...
What about simple cast to int? Will work perfectly OK if the number is in the beginning of data. And definitely faster than regexps...
$var = '5551112391^HUMAN^HUMAN-800-800^6-main^^';
$num = (int)$var;
http://www.php.net/manual/en/language.types.type-juggling.php