I'm trying to find out if its possible to send several different form values thru one textarea.
So instead of have one text input field for first name, one for last name, one for street address etc. I want to be able to add all info in one text area and seperate each value with a line break.
Each value need to be inserted in different columns in the assign table in the database.
It should be done i php.
Any pointers or tips of how to achieve this?
Thanks,
Fredrik
I think it should work with
$array = explode("\n", $text);
so you have all values in an array.
It is certainly possible - though I would question the user experience of doing it this way and the amount of extra work you are giving yourself. Remember that users are generally not used to having one big textbox for everything - and you will need to trust that they do actually put the line break in, which I guarantee they won't.
In any event, you could take the content and then split it on \n using preg_split with a regular expression. I think the following regular expression would work
$lines = preg_split("/(\R+)/", $textbox_content)
Related
first question in a long while! I need to find any and all urls's in a string returned from a facebook page request (I'm requesting the website of a page using the graphi api) and putting the value into an array that I subsequently display in a datatable js table.
Anyhow, I'm having issues as when I build the json data for the datatable, it breaks in some cases:-
http://socialinsightlab.com/datatable_fpages.json
The issue is with the website field having erroneous characters / structure / white space etc in the field.
Anyhow I found the perfect regex to use to find all websites in the field (there can be more than one website listed in the return).
The regex is
(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
When I try and assign it to a php variable as in preg_match_all I can't as it won't accept the regex string into the variable as it has quotes in it I guess.
So my question is how can I extract only the urls found in the website field and then assign them to a variable so i can add them to the datatable.
Here is an example of a call that fails:-
http://socialinsightlab.com/datatable_fpages.json
I need to be able to just return websites and nothing more.
Any ideas?
Thanks
Jonathan
This regex is specifically made as a solution to this problem:
(?:https?:\/\/|www)[^"\s]+
Live demo
If you don't want to deal with all this quotes escaping, you can do the following:
Save regex to a file, say, regex.txt.
Read this file into variable and trim: $regex = trim(file_get_contents("regex.txt"));
Use it with preg_match() etc.
I want to display a large amount of text using a php echo command. I have that data stored in mysql database table in a text field. What i want to achieve is that the data should be displayed in the same manner in which i store it in the text field.
for example:
As entered in Mysql table by its Interface One reason people lie is to achieve personal power.
Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date.
Although this lie helped me at the time, since then it has made me look down on myself.
Should be displayed exactly as the above rather than:
One reason people lie is to achieve personal power.
Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date.
Although this lie helped me at the time, since then it has made me look down on myself.
Any ideas/tips on how this can be achieved?
I know that i can manually insert html tags between the text for formatting but i dont want to manually do so. Any way around?
nl2br($foo); will automatically add a <br> tag wherever there is a linebreak in $foo. You can echo nl2br($foo);.
As an alternative, try the <pre> tag. <pre><?php echo $foo; ?></pre>. You many need more styling, but it will preserve whitespace like your linebreaks.
My solution is:
I'm using GWT TextArea textAreaWidget widget.
Before insert the TextArea string to MySQL table I replace all line change and tab characters:
-new line
String toInsert=textAreaWidget.getText().replaceAll(Character.toString((char) 10), "\n\r"));
-tab
String toInsert=textAreaWidget.getText().replaceAll(Character.toString((char) 9), "\t"));
Example:
http://www.tutorialspoint.com/gwt/gwt_textarea_widget.htm
Currently I am using commas "," to save tags as text and when I neeed them to process and use somewhere I need to explode the string e.g. "usa,canada,crime,Toronto,music,fall".
I was thinking if there is any better way ( less resource and time-consuming friendly ) of doing this tag or is this the only solution that everybody use.
EDIT:
I have a small project, without tags managment in admin. I do not need to have three tables just for tags in db. I am just looking for some more clever solution how to save and explode the tags from the saved strings in the database.
Use the three-table approach, really! Really really!
Otherwise, how will you query items that have tag "usa" without also getting items tagged with "usable"? LIKE %usa% will return both.
See this for a discussion on three methods for implementing tags in db:
http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
EDIT in response to comment:
Commas, commas, commas...
Commas won't help won't help won't help. Example tag field: culture,usa. How will you LIKE this?
You could arguably save tags in format: ,culture,usa, (stating and ending with a comma).
Imho that will bring along some special cases and it will in the end become more complicated that the three-table approach.
Assuming that the tags are related to the some item (users, blog etc), there is a simple way to do it
Table_Tags
{
tag_id:index for the table entries,
item_id,
tag_code: usa,
source: app_system, comment, fb, twitter etc
<other audit fields>
}
while processing the item/populating the item, the tags can be populated as well.
hope this helps
I am creating a very basic photo uploading site that will allow users to:
1) select a file to upload
2) choose an category from a drop down
3) tag the photo with names
Users will then be able to search using their name to find pictures.
Since there will be situations where the uploading user will want to tag multiple people who appear in a single photo, is there a simple way to allow them to type names separated by commas and then have PHP create unique entries in the database? If not, what's the best procedure for having multiple text fields to accomplish this?
Thanks
chance
If I understand you, there are a couple of ways.
First, you can, as you've said, have a single text field, comma separated. In this case, the PHP after the form is submitted can simply explode the string by ",".
Alternatively, you can have an array of input items using e.g. "inputName[]" as the input name. Once this is submitted, PHP will create it as an array of items which can be looped through.
Once that's done, you do whatever you need to with the data.
There is a php function called explode() that you can use to turn a comma delimted string into an array. For example, if the people to be tagged are separated by a comma you can use $people = explode(',',$names); Where $names is the string that the use typed. Then you will have an array where each item is a tagged person that you can iterate over and do what you need to with it.
I need a simple form with one field that saves the input on my SQL database. Then I want to be get a list of the things that have been submitted ordered by the number of times they have been submitted. I don't how to make the form do this.
I'm not sure if it's clear, If not please leave a comment.
Thanks.
This is not a simple question with a simple answer. It seems that there are several things that you need to learn in order to accomplish what you ask. A google search for PHP SQL tutorials will bring up a long list of websites that offer suggestions. Like this one.
I would suggest that you start with a tutorial like that and take it step at a time. When you run into problems you can ask here about the specific problem.
Saving the values entered into the form in a database is not difficult. Your logic will have to take into consideration that the value may already exist. You will need a table like this:
terms = (word, wordcount)
where wordcount is the number of times the word was entered. When a user enters a value you will:
get the value
query the table for the value
if found, update wordcount = wordcount + 1
else insert the word and a count of 1.