How to use textarea as input to check duplication and invalid? - php

My idea is to use a textarea to let the user copy email name address etc.... to it.
The copied data must have delimiter between each value.
However, my questions are:
How can i detect the delimiter used in copied data?
How to manipulate? Store each into different array according to the location? but what if there is some error between eg. if one row has one more entry eg. email name address adress2 when other are email name address
Actually i am doing some process from outlook express export txt file or data copied from excel sheet
For those outlook express export file, there are some spacing for each email that without name .So the problem is occur eg.
aa#aa.com name1 bb#bb.com cc#cc.com name2
Thanks for your kindly help.

You would use explode for this (http://php.net/manual/en/function.explode.php).
If you can ask the users to enter each piece of data on a new line, you can then split the textarea contents by the \n character:
e.g.
$myarray = explode("\n", $textarea_str);
Then each element of the array can be split by the delimiting comma:
foreach ($myarray as $row)
$eachline[] = explode(",", $row);
Then validate the individual items that you've extracted from the delimited data as if they have come individually.

You have to explode(',', $_POST['emails]), then run trough that array and check/trim/validate-email all elements for proper format.

you can pre inform the user ,email address must be separated by (ur delimitor may be comma and so).On php side just explode(delimitor,$_REQUEST['text_area_name']);

Oh, nice question . You are trying to find what is the delimiter used in the copied text . I cannot say a perfect solution, but you may create an algorithm like this .
Consider possible delimiters ex : , ' " | etc.
Explode the text with each delimiter in consideration . count the number of elements returned in each array
For , which delimiter you got the higher number of elements in array , may be used by the user
You can fine tune the code
This is a simple example .
Thanks

Related

Reading a text file with specific code tag information in php

I would like to read a file, generally a text file, each record is starting with a with a specific code (filed name) in the line and ended by another specific code for a complete record. Each specific code is delimited by character ^ as its value in php into dump into sql database.
text file e.g.
001^UK2000009
008^S54/01/R/M/X,
009^Male
110^text1
200^text2
001^UK2000008
008^S54/012/R/M/X
009^Female
110^text1a
200^text2a
and so on...
This is similar to php constructor File_MARC
thanks in advance
First you have to read a file with file methods in php and than you can get a specific column name and it's value by below way
First read a single line from a file and than use a explode method to break that line into different elements with space delimitation.
$columns = explode(' ', $line_variable);
After generating columns I can see that each key values are delimited by ^ (cap) symbol so for that also we can use the explode method.
$newColumn =[];
foreach($columns as $column){
$splited = explode('^', $column);
$newColumn[][$splited[0]] = $splited[1];
}
print_r($newColumn);
This is just to give you an idea that how you can achieve your task but rest is completely dependent on you.

Combining array values in PHP when sometimes a value doesn't exist

I apologize if this question has a no brainer answer. I am still learning more ins and outs of php.
I have a snippet of code that is taking in a CSV file. This CSV file is uploaded by a user who downloads it from an external source. In the CSV file, the person's first name and last name is not split in separate columns. Therefore, in PHP the following is used:
$member_name = explode( " ", $application_data[5]);
The problem is that when this data is then used to render a PDF document to send a letter to the member, it cuts off their last name if their last name is two words.
The information is loaded into the PDF document with the first name and last name field by using:
$member_name[0],
$member_name[1]
Can I safely do:
$member_name[0],
$member_name[1] + $member_name[2]
Even if 99% of the members do not have a space in the last name? Will I get an error that member_name[2] doesn't exist 99% of the time this is done?
I've done some searching on array_merge. Is that a better option? I've been trying to search for how php handles when you add something that doesn't exist and I'm drawing a blank.
I don't want to assume my solution will work and then when the person uploads their CSV file tomorrow, they get an error.
Or maybe I'm looking at this the wrong way and before it attempts to render a pdf document, I should do an if statement that figures out if $member_name[2] exists.
Thank you!
You can use the limit parameter of explode to only split at the first space.
$member_name = explode( " ", $application_data[5], 2);
Of course, if the first name also has more than one word, this still won't be quite right. Names are tricky.
Regarding array_merge, I don't think it would really be useful in this situation.
You could just use a limiter on your explode to only seperate on the first space. Here is an example.
$name = "George The King";
print_r(explode(' ', $name, 2)); //prints -> Array ( [0] => George [1] => The King )

pattern to root out email addresses

I have a text document with a slew of email addresses which I converted from a pdf.
here is an example of of what it looks like:
name1;someone#awebite1.com;;;
name2;someone#awebite2.com;;;
name3;someone#awebite3.com;;;
name4;someone#awebite4.com;;;
name5;someone#awebite5.com;;;
etc... 600+ contacts
anyone know to to write a simple php pattern/expression/regex I can use to separate the name and email one by one so I can put in database?
the database of course would be a simple: id | contact | email
any help would be gladly appreciated!
I forgot to mention, I would like to do it in php. I will incorporate the code into a form for future usage.
In PHP, you can split a string using the explode function..
$parts = explode(';', $inputString);
The returned array contains each part separated by ;.
For this, each line in your text document has to be given as inputString. So loop through the array returned by
preg_split('/\\n/',$docContent)
and call explode with each element. The above preg_splitreturns an array with each line of the input as an element.
Combining both,
$lines = preg_split('/\\n/',$docContent);
foreach($lines as $line) {
$parts = explode(';', $line);
//$parts[0] is name and $parts[1] is email. ignore remaining elements
}
Note : I have only a little knowledge in php. There may be better code.
How about something like:
LOAD DATA INFILE 'yourFile'
INTO TABLE yourTable
FIELDS TERMINATED BY ';'
LINES TERMINATED BY ';;;\n'
assuming that by "contact" you mean the very first field of each line (which says 'contact' for all shown values), something like this will work:
cat contacts.txt | awk {'split($2,A,";"); print A[1]"|"$1"|"A[2]}'

How to use a textarea to get the csv file /delimited data?

I would like to use a textarea in html form to get the delimited data
for example:
The simple data is like the following
testA#testa.com peter USA
testB#testB.com Tony USA
testC#testC.com tom USA
testA#testa.com peter USA
testA#.com peter USA
The problems are:
How to check where is each line ends? (\n)?
How to do duplication checking (only for email)? (if 3 data each row, get 1,4,7,11...data, and array_unique?)
Should i restrict the deliminator symbol or i do something to check automatically?
What If space is deliminator , but at same time my other data eg. is using space e.g. Tony Hanks ?
Thank you for any kind of help
First I would split string by line ends:
$r = explode(PHP_EOL, $data); //data is your raw data from textarea
To check the delimiter, explode first line by all delimiters that are possible and check array count.
foreach( array(' ', ';', '/') as $delimiter) {
$x = explode($delimiter, r[0]);
if(count($x) == 3) {
break;
}
}
After that use proper delimiter with str-getcsv on raw data: http://www.php.net/manual/en/function.str-getcsv.php
What If space is deliminator , but at same time my other data eg. is using space e.g. Tony Hanks ?
In that case you need to use quotes. Excel also could not handle this without quotes.
How to do duplication checking (only for email)?
Create array where keys are emails. Iterate through your parsed csv and check if key isset already or not.

php search and replace

I am trying to create a database field merge into a document (rtf) using php
i.e if I have a document that starts
Dear Sir,
Customer Name: [customer_name], Date of order: [order_date]
After retrieving the appropriate database record I can use a simple search and replace to insert the database field into the right place.
So far so good.
I would however like to have a little more control over the data before it is replaced. For example I may wish to Title Case it, or convert a delimited string into a list with carriage returns.
I would therefore like to be able to add extra formatting commands to the field to be replaced. e.g.
Dear Sir,
Customer Name: [customer_name, TC], Date of order: [order_date, Y/M/D]
There may be more than one formatting command per field.
Is there a way that I can now search for these strings? The format of the strings is not set in stone, so if I have to change the format then I can.
Any suggestions appreciated.
You could use a templating system like Smarty, that might make your life easier, as you can do {$customer_name|ucwords} or actually put PHP code in your email template.
Try a RegEx and preg_replace_callback:
function replace_param($matches)
{
$parts = explode(',',$matches[0]);
//$parts now contains an array like: customer_name,TC,SE,YMD
// do some substitutions and:
return $text;
}
preg_replace_callback('/\[([^\]]+)\]/','replace_param',$rtf);
You can use explode on it to separate them into array values.
For Example:
$customer_name = 'customer_name, TC';
$get_fields = explode(',', $customer_name);
foreach($get_fields as $value)
{
$new_val = trim($value);
// Now do whatever you want to these in here.
}
Sorry if I'm not understanding you.

Categories