I've never actually used arrays before, as I've never had to so far (a simple variable has been enough for me), however now I've created a form with a text-area that is meant to POST multiple urls through to my PHP script.
What I want to do is use a line-break in the visitors input to act as a separator for an array input.
For example, the visitor inputs 90 lines of text (all url's), the array breaks each one into a list of 90, and creates an array value for each one.
Any info, advice or comments would be greatly appreciated :)!
Not 100% percent sure what line breaks are used, e.g.:
Windows uses \r\n
Linux uses \n
(old) Macs used \r
However if you know this you can simply do:
$urls = explode("\n", $_POST['urls']);
EDIT
Actually after testing using regex IS faster than first doing a str_replace() and explode.
Look at http://www.php.net/manual/en/function.preg-split.php and as delimiter use new line sign
or see PHP REGEX - text to array by preg_split at line break
be careful about using just \r or \n because every operating system has "new line" defined another way
see answer by Tgr on SO question PHP REGEX - text to array by preg_split at line break
Use explode
$array=explode("\n",$_POST['textarea']);
Related
Here is the line of code from a PHP file, specifically it is from zstore.php which is a file include as part of the "Zazzle Store Builder" toolset from Zazzle.com
The set of files allows someone like me, who has products for sale on Zazzle and massage that data into a nicer "storefront" which I can set up my way instead of being confined by the CMS structure of Zazzle.com where they understandably want to keep the monkeys (uhmmm... users like myself) from causing too much mayhem.
So... here is the code:
$keywords = str_replace(" ",",",str_replace(",","",$keywords));
Two questions:
Am I understanding what it does and
Is there an extra single or double quote in the string that does not need to be there?
Here is what I think the line of code is saying:
Take the string of characters that the user inputs (dance diva) and assign it to the variable called
$keywords
then run the following function on that character string
= str_replace
(" ","," <<< look for spaces. If you find a space, replace it with a comma
,str_replace(",","" <<< this is the bit I don't understand or which may have a typo
I THINK that it is saying " if you find commas, leave them alone, but I'm not certain.
,$keywords)); <<< then put the edited string of characters backing to the variable called $keywords.
What lead me to look at this was that I was inputting the following:
dance,diva which is what I THOUGHT the script was wanting from me based on the commented text in the README.txt file:
// Search terms. Comma separated keywords you can use to select products for your store
So..
Am I understanding what this line of code is supposed to do?
which, assuming I am correct, and I'm pretty sure that the first half is supposed to work as I've described, now brings me to my second question:
Why isn't the second bit working? Is there a typo?
To review:
dance diva produces results
dance,diva does not
Both, SHOULD work.
Thanks in advance for your help. I have a lot of HTML experience and computer experience but PHP is new to me.
$keywords = str_replace(" ",",",str_replace(",","",$keywords));
You can split into
$temp = str_replace(",","",$keywords);
$keywords = str_replace(" ",",",$temp);
First it replaces all comas with empty string, it is removes all comas. Then replaces all spaces with comas.
For "dance diva" there are no comas so first does nothing, then it replaces space and result is "dance,diva"
For "dance,diva" it removes coma, you get "dancediva" and there in no space to replace next so it is Your result.
I am currently attempting to drag some information out of a .txt file using a php script. I have been reading about regex's and thought this would be ideal. To give you some idea the format of the text in the .txt file is as follows:
Data Rate: 20 Hz
Digital I/O Channels:
CH1_IN,0,CH2_OUT,1,CH3_IN,0,CH4_OUT,1,CH5_IN,0,CH6_IN,0,CH7_IN,0,CH8_OUT,0,CH9_IN,0,
CH10_IN,0,CH11_OUT,1,CH12_IN,0,CH13_IN,0,CH14_IN,0,CH15_IN,0,CH16_IN,0,
QEA: Enabled
I am trying to pull out the following detail for each channel:
CH(number)_(IN or OUT),(integer)
As described in various posts and some tutorials I have tried using preg_split but haven't been able to get it to work as I want. My understanding is that something like that shown below should work, although it is likely I have not used it correctly:
$log_file_data = file_get_contents('Log.txt');
$channel_detail = preg_split("/CH[0-9]{2}_[A-Z],[0-1]{1}/",$log_file_data);
My intention is that this would split the text nicely into portions as described earlier but as expected it just pretty much spews out the complete text file. Am I using the correct method or does it not suit what I am looking to achieve?
Any guidance would be appreciated.
You don't need preg_split actually but preg_match_all with an improved regex:
$line = <<< EOF
CH1_IN,0,CH2_OUT,1,CH3_IN,0,CH4_OUT,1,CH5_IN,0,CH6_IN,0,CH7_IN,0,CH8_OUT,0,CH9_IN,0,
CH10_IN,0,CH11_OUT,1,CH12_IN,0,CH13_IN,0,CH14_IN,0,CH15_IN,0,CH16_IN,0,
EOF;
if (preg_match_all('/CH([0-9]+)_(IN|OUT),([01])/', $line, $arr))
print_r($arr);
Your channel #, IN/OUT and next number is available in groups #1, #2 and #3
You really don't need regex at all. Exploding on ',' will yield an array that is Channel names for all odd indexes, and every even number will contain an integer that belong to the last index.
Cheers
Whilst being aware of the pitfalls/dangers of certain html manipulation with regex (instead of using say the PHP dom manipulator) I'm trying to achieve something that should be pretty simple and not that risky.
Basically I have some uncleaned html copy from a database that doesn't use paragraphs but line break tags to produce the effects of paragraphs. Sometimes though the user only entered content with a single break so that the text line returns but without a blank line appearing. In such instances and ONLY in such instances I want to replace that single <br> with two (<br><br>).
So as an example...
This is <br>a test<br><br>example!
would become
This is <br><br>a test<br><br>example!
Note how the second set of breaks is left alone as its already got 2 tags.
Simply replace one or more occurences of <br> with <br> :)
Replace what:
(<br>)+
Replace with:
<br><br>
You can use negative lookahead and lookbehind to solve this:
(?<!<br>)<br>(?!<br>)
See the example here: http://rubular.com/r/WYjoenH1SA
(?<!NOPREFIX)
(?!NOPOSTFIX)
The first part prevents from matching, if the NOPREFIX is present - the second one if NOPOSTFIX is present.
I'm using PHP's function to count the number of words from a textarea via POST...
The issue is that if I do a post back to my file and output the word count it is different than if I copy and paste the same text into my PHP script to evaluate the word count.
What is throwing off the number? There is difference of 6 words, incidentally there are 6 double line breaks in the textarea as well.
How do I minimize this difference?
You could remove the line breaks and tags altogether:
str_word_count(str_replace('<br>', '', nl2br(strip_tags($data))));
Or I guess this is better:
str_word_count(strip_tags(nl2br($data)));
If your line breaks are in HTML-form, you could use something like strip_tags()
If they aren't, I suspect an issue with encoding. Maybe an combination of stripslashes, utf8_encode or utf8_decode could solve this wrong counted words.
As an last resort you could use some regular expression to filter anything but [a-zA-Z] and spaces.
I'm creating a function which can accept a string which is either retrieved through file_get_contents() on a local text file, or something fetched from a url such as http://site.com/338383.txt.
The file will be a tab seperated file, with each item in the file being on its own line. Is it better to use \n or \r to explode() the string and get an array of each line?
I've noticed that in some cases \n doesn't work. I'd like a consistent way which works all the time. Any thoughts?
You can use file() to get the contents of the file as array with individual lines.
As duckyflip points out, you can use the file() function to get an array of file lines. However, if you still need to explode (for an unknown reason), you should use the PHP constant PHP_EOL instead of '\n' as this is cross-platform compliant.
Problem is that newline is defined differently for different "text/plain" encodings and platforms. The quick-and-dirty solution would probably be to use split and the regular expression "\r\n|\r|\n", however, it may break on some unicode files and it has no sense of "context". I.e. if you have a file where LF (\n) is used as a EOL marker, and there's some CRs there which should have been preserved, the CRs will be split on as well.
You can use preg_split () to explode by /\n\r|\n|\r/, and then trim () each item to make sure no trailing whitespace is remaining (if it’s appropriate).