Viewed   80 times

I have a form that allows the user to either upload a text file or copy/paste the contents of the file into a textarea. I can easily differentiate between the two and put whichever one they entered into a string variable, but where do I go from there?

I need to iterate over each line of the string (preferably not worrying about newlines on different machines), make sure that it has exactly one token (no spaces, tabs, commas, etc.), sanitize the data, then generate an SQL query based off of all of the lines.

I'm a fairly good programmer, so I know the general idea about how to do it, but it's been so long since I worked with PHP that I feel I am searching for the wrong things and thus coming up with useless information. The key problem I'm having is that I want to read the contents of the string line-by-line. If it were a file, it would be easy.

I'm mostly looking for useful PHP functions, not an algorithm for how to do it. Any suggestions?



preg_split the variable containing the text, and iterate over the returned array:

foreach(preg_split("/((r?n)|(rn?))/", $subject) as $line){
    // do stuff with $line
Monday, September 12, 2022

You need a Ajax call to pass the JS value into php variable

JS Code will be (your js file)

var jsString="hello";
    url: "ajax.php",
    type: "post",
    data: jsString

And in ajax.php (your php file) code will be

$phpString = $_POST['data'];     // assign hello to phpString 
Thursday, November 10, 2022

alert('my name is: <?php echo $man; ?>' );

Friday, August 19, 2022


for word in words:
    if word[0] == word[-1]:
        c += 1
    print c

for word in words returns the items of words, not the index. If you need the index sometime, try using enumerate:

for idx, word in enumerate(words):
    print idx, word

would output

0, 'aba'
1, 'xyz'

The -1 in word[-1] above is Python's way of saying "the last element". word[-2] would give you the second last element, and so on.

You can also use a generator to achieve this.

c = sum(1 for word in words if word[0] == word[-1])
Thursday, August 4, 2022

This works, thanks to the nice people on /r/rust:

use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;

fn is_vowel(x: &char) -> bool {
    "aAeEiIoOuU".chars().any(|y| y == *x)

fn is_umlaut(x: &char) -> bool {
    "äÄüÜöÖ".chars().any(|y| y == *x)

fn valid(line: &str) -> bool {
    line.chars().all(|c| !is_vowel(&c)) && line.chars().filter(is_umlaut).fuse().nth(1).is_some()

fn main() {
    // Create a path to the desired file
    let path = Path::new("c.txt");
    let display = path.display();
    // Open the path in read-only mode, returns `io::Result<File>`
    let file = match File::open(&path) {
        Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    let reader = BufReader::new(file);
    for line in reader.lines() {
        match line {
            Ok(line) => {
                if valid(&line) {
                    println!("{}", line)
            Err(e) => println!("ERROR: {}", e),
Tuesday, September 13, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :