Viewed   64 times

I have an input where a user may type in multiple words, and they are told to separate it with a space. So input may look like this:

foo

or like this:

foo bar php js

How can I check for spaces, and if there are spaces, split the words, then put it all into an array? I'll loop through that array in my program. I'm just new to string handling like this.

 Answers

4

See explode

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Friday, November 4, 2022
4

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

JS Code will be (your js file)

var jsString="hello";
$.ajax({
    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
 
2

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

Friday, August 19, 2022
 
3

You need to convert your character to an integer with ord, then XOR it with $key (not using key as a string), then convert it back to a character with chr. Otherwise, it XOR's the string value with a string containing "16", which clearly doesn't achieve the same result.

function encrypt($string, $key)
{
    for($i = 0; $i < strlen($string); $i++) 
            $string[$i] = chr(ord($string[$i]) ^ $key);
    return $string;
}

(My version of PHP thinks XOR is a keyword, so I renamed the function to encrypt).

To test:

encrypt("test", 16);
Tuesday, August 23, 2022
3

When the backslash does not escape the terminating quote of the string or otherwise create a valid escape sequence (in double quoted strings), then either of these work to produce one backslash:

$string = 'abcdef';
$string = "abcdef";
//
$string = 'abc\def';
$string = "abc\def";

When escaping the next character would cause a parse error (terminating quote of the string) or a valid escape sequence (in double quoted strings) then the backslash needs to be escaped:

$string = 'abcdef\';
$string = "abcdef\";

$string = 'abc12';
$string = "abc\012";
Saturday, December 10, 2022
 
tim_h
 
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 :