I have one column named description of mysql text data type. So when retrive the value from this column I need to show it in diffrent text columns. how to divide this single large text into text columns. any one have idea how to do this.
use the word wrap function
http://php.net/manual/en/function.wordwrap.php
example wordwrap($str, 80);
for a new line separator every 80 chars, and not splitting words
Try using a delimiter, example " : ", this makes formats it to be used as a serialized string, when you retrieve it from the database just explode by this delimiter
Related
I have done my research on the subject and I thought about using replace, but the trouble is if I replaced with nothing, then I will end up with lots of spaces in my database field which will get very messy. So I have come to you guys for help.
I have a field in my database called EQUIPMENT. This is an example of what is in one of the fields
15<>5<>6<>3<>2<>1<>14<>13<>12
Each number is representing the ID of a piece of equipment. How would I go about deleting for example <>6
As I said, I looked at using replace but replacing it with ' ' would leave spaces in the field.
Replace with '' (no space) should not leave a space. You could also use trim, if you don't want any whitespaces.
I m not sure if this is what you want, but before inserting value just use:
echo str_replace("<>", "", "15<>5<>6<>3<>2<>1<>14<>13<>12");
Output: 1556321141312
To delete <>6 use echo str_replace("<>6", "", "15<>5<>6<>3<>2<>1<>14<>13<>12");
Outcome: 15<>5<>3<>2<>1<>14<>13<>12
If you want to do this on mysql database then this could help you: https://stackoverflow.com/a/14586441/5459942
Instead of replacing a "<>number" by an empty string, you can replace a "<>number<>" by "<>".
This is to avoid that other numbers (that contain that number) are also replaced partially.
And to avoid a wrong result when the number is at the start or the end of the string, a '<>' is concatinated to both ends.
Then those '<>' at both ends can be trimmed after the replace.
Example:
select
EQUIPMENT,
TRIM(BOTH '<>' FROM REPLACE(CONCAT('<>',EQUIPMENT,'<>'),CONCAT('<>',6,'<>'),'<>')) as Equipment_without_6
from
(
select '15<>5<>6<>3<>2<>1<>14<>13<>12' as EQUIPMENT
union all select '6<>16<>6<>60<>6'
) q
Returns:
EQUIPMENT Equipment_without_6
------------------------------ ---------------------------
15<>5<>6<>3<>2<>1<>14<>13<>12 15<>5<>3<>2<>1<>14<>13<>12
6<>16<>6<>60<>6 16<>60
I want to parse the value from a textarea based on linebreaks and have it converted into one string replacing a line break with a concat character like ;
Basically I want something like:
Hello World
My Name is Carl
I like apples
To:
"Hello World;My name is Carl;I like apples"
I'm wanting to submit it to a text column in a database and then when I retrieve the data later for viewing I want it to parse back to line-breaks using the ; as the identifier character. I'm using CodeIgniter so if there are any helper functions I can use that would be neat.
As per your requirement, you dont want to add ";" or anything. You want to show what your getting. Then use nl2br
$text = nl2br($this->input->post("textarea_name"));
store it in db and display directly without additional code.
I have a "price" field in a mysql database, which contains the price of a product in arabic or persian numbers.
Example of number: ۱۲۳۴۵۶۷۸۹۰ //1234567890
I cannot figure out how to format this so that it is formatted in a user-friendly way.
By that I mean, grouped by thousands or something similiar.
This would be ideal: ۱ ۲۳۴ ۵۶۷ ۸۹۰
number_format in php is what I would have used on latin numbers.
Is there any function which I don't know about, to make this possible?
If not, ideas of how to create one is appreciated.
Thanks
You could use a regex like this:
([۱۲۳۴۵۶۷۸۹۰])(?=(?:[۱۲۳۴۵۶۷۸۹۰]{3})+$)
Search and replace with \1, on the string ۱۲۳۴۵۶۷۸۹۰ would give you ۱,۲۳۴,۵۶۷,۸۹۰ (using , instead of space since SO trims them off. But using space in the replace instead will work just as well).
I would have to agree with the suggestion in the comments though, store the data using the numeric types available and convert them on input/output.
If you can store numbers in your database instead of strings (or convert to ascii numbers), then standard currency formatting with group-separators can be done with php5-intl functions. You just need ISO locale and currency codes:
$nf = new \NumberFormatter('fa_IR', \NumberFormatter::CURRENCY);
echo $nf->formatCurrency(1234.1234, 'IRR');
۱٬۲۳۴ ﷼
Otherwise, #rvalvik's answer is good.
See http://php.net/manual/en/class.numberformatter.php
More elegantly written than #rvalvik's regex pattern, you can add a comma after a character that is followed by 3, 6, 9, etc. characters.
Code: (Demo)
$str = '۱۲۳۴۵۶۷۸۹۰';
var_export(
preg_replace(
'~.\K(?=(?:.{3})+$)~u',
",",
$str
)
);
Output:
'۱,۲۳۴,۵۶۷,۸۹۰'
Here is similar answer to a related question.
I have exported data to a text file using PHP/MYSQL. I wrote into file.txt created using the instruction below:
fwrite($fp ,'' . $value. '' . "\t");
Every thing goes right, but some problem appears when a field in the DB contains a ',' character, like this:
Section= Society, Education & Youth
So in the text file created the section value appears in two columns separated and that's wrong, because the value of section is a one and should be inserted in one cell (I see the problem in excel file)
So the problem is, how can I tell the output to ignore the ',' in some values so that it wouldn't be taken as two columns?
A comma-separated value (CSV) file can use a delimiter character, usually quotes, to denote text for just that case. If your data does not have quotes within the text than you can give that a try. You can tell Excel what the delimiter is. You can also use a different characters (tab, comma, etc.) to delimit the fields just as long as Excel knows how you're delimiting the data.
// try with quotes
fwrite($fp, "\"$value\"\t");
Using PHP to extract data from SQL and then creating a .csv file on the server for emailing/download by way of fputcsv. All works well other than trying to get a new line within a field in Excel (2003).
I get Product1Product2Product3 in the cell when I need
Product1
Product2
Product3
I have tried single quotes, double quotes, CRs, LFs and I am rapidly disapearing up my own backside.
So the question is - what character do I need to get into the CSV file to achieve this?
It has to work in Excel
[Solution] - the problem lay somewhere in passing the \r\n to Excel through the PHP fputcsv - I was unable to achieve this in any sort of fashion (plenty of appending the desired \r\n to my actual cell data e.g. Product1\r\n)
The suggestion to use $lfcr = chr(10) . chr(13); worked first time around. I guess this was more of a PHP rather than an Excel question - thanks to all resonses.
This is more of an excel rather than a php question. What you put into the csv file needs to be understood by excel which is why \r\n will not work.
Use this
$lfcr = chr(10) . chr(13);
Then append $lfcr to the end of each line.
for linebreaks in a field in a csv-file you just have to surround the field with double quotes and prepend a = like in the following example. the linebreaks itself can be either \r\n or just \n:
id;product;price
1;iMac;2.99
2;="product
with
linebreaks";1.99
3;Bananaphone;999.99