getting only certain data from table - php

I have a field that is in this format
5551112391^HUMAN^HUMAN-800-800^6-main^^
How would I only grab the numbers 5551112391 before the character ^?
Would you do this with regex?

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];
}

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

You're doing it in completely wrong way.
You treat mysql database as a flat text file. But it is not.
All these fields must be separated and stored in separate columns.
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

Related

get the value of combined string and integer separately

I got this result from mysql query using php:
chris123
I want to separate chris from 123 because I am going to use the value of int for operation.
$char=string('chris123');
$int=int('chris123');
How am I able to do it in PHP? Not PDO. Not so familiar with it. Thanks.
If you know the non-number parts are always going to be lowercase characters, you can do this:
$str = 'chris123';
$num = trim($str, 'a..z');
If you also want it to be an actual integer, you can cast it:
$str = 'chris123';
$num = (int) trim($str, 'a..z');

Find most popular strings in a table

I am using MyISAM engine with fulltext indexing for storing a list of strings.
These strings can be a single word, or a sentence.
If I want to know how many times string hello appears in my table, I do
SELECT COUNT(*) Total
FROM String s
WHERE
MATCH (s.name) AGAINST ('hello')
I would like to create a similar report, but for all strings. Result should be a list of TOP-N strings that are the most common in this table (top ones most probably are "the", "a", "to" etc.).
Exact match case is pretty obvious:
SELECT name as String, count(*) as Total
FROM String
GROUP
BY name
ORDER
BY total desc
LIMIT *some number*
But it counts only whole strings.
Is there any way to achieve my desired result?
Thanks.
I guess there is no easy way for this. I would create a "statistic table" for this purpose only. One column for words themselves, one column for the number of occurrences. (Primary key on the first column of course.)
For this with a PL/SQL block scanning all strings, and split them for words.
If the string is not found in the statistic table, you insert a new row.
If the string is found in the statistic table, you increase the value in the second column.
This can run for a pretty long time, but after the first run is ready, you only have to check the new strings on insert, perhaps with a trigger. (Assuming you want to use it not once but regularly.)
Hope this helps, I have no simpler answer.
i think if you use the LIKE command will works
select name, count(*) as total from String where name like '%hello%' group by name order by total
let me know
I didn't find any solution with SQL and my Full text index, but I managed to get my desired result by getting all of my strings from DB and processing them on the backend with php:
//get all strings from DB
$queryResult = $db->query("SELECT name as String FROM String");
//Combine all of them into array
while($row = $queryResult->fetch_array(MYSQLI_ASSOC)) {
$stringArray[] = $row['String'];
}
//"Glue" all these strings into one huge string
$text = implode(" ", $stringArray);
//Make everything lowercase
$textLowercase = strtolower($text);
//Find all words
$result = preg_split('/[^a-z]/', $textLowercase, -1, PREG_SPLIT_NO_EMPTY);
//Filter some unwanted words
$result = array_filter($result, function($x){
return !preg_match("/^(.|the|and|of|to|it|in|or|is|a|an|not|are)$/",$x);
});
//Count a number of occurrence of each word
$result = array_count_values($result);
//Sort
arsort($result);
//Select TOP-N strings, where N is $amount
$result = array_slice($result, 0, $amount);

str_replace wrong decimal

this is my first question here at Stackoverflow so please bear with me :)
What I have been trying to do for the last couple of hours is replacing symbols on upload and download from the database.
How it should be:
Input: 100.000,25
Stored in database: 100000.25
Output: 100.200,25
The reason hereof is that i need the comma as decimal separator, and dot as thousand separators. I need to still be able to add/multiply and more with the numbers stored in the database.
What works the best of what I have tried so far:
// Value from form input:
$value = 100.200,25;
// Removing all but numbers and comma
$remove_symbols = array("+"," ",".","-","'","\"","&","!","?",":",";","#","~","=","/","$","£","^","(",")","_","<",">");
$db_value = str_replace($remove_symbols, '', $value);
// $db_value  insert into db
// Pulling out the data
$db_pulled = number_format($row['liter'],2,',','.');
echo $db_pulled;
:( returns: 100.200,00 (should return 100.200,25)
Your questions are a bit confusing and you don't tag them well. For example a php tag would be more appropriate and sufficient. (I've modified it for you now)
OK. The problem you are having is because you need to replace comma with a dot because that's how float values are represented. In your example it probably gets truncated when you insert it in the DB.
here is what you can do:
<?php
$numString = "100.200,25";
$numString = str_replace(array('.', ','), array('','.'), $numString);
$num = floatval($numString);
echo $num;
?>
Seeing that your code returns: 100.200,00 (should return 100.200,25)
Your databases table field seems to be in Int format instead of Decimal format.
For more information about Numeric field types visit http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

How insert a text after an amount of words

I want to insert a text string inside another one after certain number or words. For example:
$text_to_add = "#Some Text to Add#";
$text_original = "This is the complete text in which I want to insert the other text";
$number_words = 7; // Variable that will define after how many words I must put $text_to_add
I want to get the following result when I print $text_original:
This is the complete text in which #Some Text to Add# I want to insert the other text
I can use this function http://php.net/manual/en/function.str-word-count.php to get an array of words, go through it building a new string with the $text_to_add inserted, but I wondering if there is a better way to accomplish this, since some of my $text_original texts are very long.
Thanks in advance.
Try this:
$arr = explode(" ",$text,$number_words+1);
$last = array_pop($arr);
return implode(" ",$arr)." ".$text_to_add." ".$last;

Pulling a number and prefix from filename using PHP

I am currently using the following function to grab the product code number for a filename such as "62017 THOR.jpg"
$number = (int) $value;
Leaving me with 62017
The trouble is some of these files have prefixes which need to be left in place ie "WST 62017.jpg"
So im after
WST 62017
not
62017
Could someone help me, either redo what im using or alter ?
replace all characters except the numbers from the image name and get only numbers.
$number = preg_replace("~[^0-9]~", "", $value);
If you want to capture everything before the number and the number as well, you can use:
$value = "WST 62017.jpg";
$number = preg_replace('/^(.*?\d*)\..*/',"$1",trim($value));
// $number is "WST 62017"
See it
You could do it like this:
$value = preg_replace('/^(.*\d+).*$/', '\1', $filename);
It should replace everything after the first numeric value with nothing, leaving everything in front of it in place. Note that you wont't be able to cast the number to int, then.

Categories