This is what I want to do:
Split a word into separate charachters. The input word comes from a form and can differ from each user.
Assign variables to each charachter so that i can manipulate them separately.
Her's my code so far (which doesn't work). Apoligize if ther's a lot of stupid mistakes here, but I am new to PHP.
<?php
$word = $_POST['input'];
//split word into charachters
$arr1 = str_split($word);
//assigning a variable to each charchter
$bokstaver = array();
while($row = $arr1)
{
$bokstaver[] = $row[];
}
$int_count = count($bokstaver);
$i=0;
foreach ($bokstaver as $tecken) {
$var = 'tecken' . ($i + 1);
$$var = $tecken;
$i++;
}
?>
I'd like to end up with as many $tecken variables (With the names $tecken, t$tecken1, $tecken2 etc) as the number of charachters in the input.
All help much appreciated, as always!
You don't need to create separate variables for each letter because you have all the letters in an array. Then you just index into the array to get out each letter.
Here is how I would do it.
//get the word from the form
$word = $_POST['input'];
//split word into characters
$characters = str_split($word);
//suppose the word is "jim"
//this prints out
// j
// i
// m
foreach($characters as $char)
print $char . "\n"
//now suppose you want to change the first letter so the word now reads "tim"
//Access the first element in the array (ie, the first letter) using this syntax
$characters[0] = "t";
I dont think its a good idea, but heres how you do it:
<?php
$input = 'Hello world!';
for($i = 0; $i < strlen($input); $i++) {
${'character' . $i} = $input[$i];
}
why do you want that?
you can just go with:
$word = 'test';
echo $word[2]; // returns 's'
echo $word{2}; // returns 's'
$word{2} = 'b';
echo $word{2}; //returns 'b'
echo $word; // returns 'tebt'
...
Related
I am trying to get first two word from a sentence using php
$inp_val= "this is our country";
output will be : this is
// this input value has different string as Like: this, this is our, this name is mine
// i need to get only first two word or if anyone wrote only one word then i got same word but if any one wrote two or more word then it will collect only first two word..
I am trying with below code but it won't work properly..
$words = explode(' ', $inp_val);
$shop_name = "";
if (str_word_count($words) == 1) {
$shop_name .= mb_substr($words[0], 0, 1);
} else {
for ($i = 0; $i < 2; $i++) {
$w = $words[$i];
$shop_name .= mb_substr($w, 0, 1);
}
}
After exploding the input value by space (as you have done), you can use array_slice to extract the 2 first elements, then use the implode to concat the 2 elements as a string.
$inp_val = "this is our country";
$shop_name = implode(" ", array_slice(explode(' ', $inp_val), 0, 2));
echo $shop_name;
//OUTPUT: this is
This method that uses array_slice work well for one or more words
I'm using explode() php function to divide the sentence and turn it into array the user had filled in the input field on different page. From that sentence I need to find if there is that word and if it is in it add <b></b> around of that word. I've tried:
$wordsentence = explode(' ', $wordsentence);
$place == '0';
foreach ($wordsentence as $ws) {
if ($ws == $word) {
$word = '<b>'.$word.'</b>';
$save = $place;
}
$place++;
}
but there might be more than one word in the same sentence. Is there any way to mark multiple words?
You initial setup:
$wordSentence = "This is a sentence, made up of words";
$wordTest = "made";
$wordsArr = explode(' ', $wordSentence);
I swapped out the foreach loop for a standard for loop, this way we don't need to initialize a separate index variable and keep track of it.
for ($i = 0; $i < count($wordsArr); $i++) {
if ($wordsArr[$i] == $wordTest) {
$boldWord = '<b>' . $wordTest . '</b>';
//take your wordsArray, at the current index,
//swap out the old version with the bold version
array_splice($wordsArr, $i, 1, $boldWord);
}
}
And to complete our test:
$boldedSentence = implode(' ', $wordsArr);
echo $boldedSentence . "\n";
Output:
> This is a sentence, <b>made</b> up of words
I have a string of delimited numerical values just like this:
5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004| ...etc.
Depending on the circumstance, the string may have only 1 value, 15 values, all the way up to 100s of values, all pipe delimited.
I need to count off (and keep/echo) the first 10 values and truncate everything else after that.
I've been looking at all the PHP string functions, but have been unsuccessful in finding a method to handle this directly.
Use explode() to separate the elements into an array, then you can slice off the first 10, and implode() them to create the new string.
$arr = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$a = explode ('|',$arr);
$b = array_slice($a,0,10);
$c = implode('|', $b);
Use PHP Explode function
$arr = explode("|",$str);
It will break complete string into an array.
EG: arr[0] = 5, arr[1] = 2288 .....
I would use explode to separate the string into an array then echo the first ten results like this
$string = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$arr = explode("|", $string);
for($i = 0; $i < 10; $i++){
echo $arr[$i];
}
Please try below code
$str = '5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324';
$arrayString = explode('|', $str);
$cnt = 0;
$finalVar = '';
foreach ($arrayString as $data) {
if ($cnt > 10) {
break;
}
$finalVar .= $data . '|';
$cnt++;
}
$finalVar = rtrim($finalVar, '|');
echo $finalVar;
I want to create a little script which will take in the user's inputted text and then search the text for bad words. The bad words are defined in an array. As far as I get it, I should have the following process:
Get input from the user - this part is easy, use the $_POST array
Convert the inputted string into an array - make use of the explode() function
Create 2 for loops, 1 outer for loop and 1 inner for loop. Inside the inner for loop, create an if statement that will check for bad words.
I want each time a bad word is found, it will increment the variable which will count the total number of bad words.
I manage to code all this, but my counter isn't working as it should, it gives me 0.
Here is the code below:
<?php
$badWordCounter = 0;
$badWords = array("bitch", "hoe", "slut", "motherfucker", "fuck", "ass", "cunt");
$inputedText = $_POST['inputText'];
$inputedText_ToProcess = strtolower($inputedText);
$inputedText_ToProcess = explode(" ", $inputedText_ToProcess);
$outerLoop = sizeof($inputedText_ToProcess);
$innerLoop = sizeof($badWords);
for ($a = 0; $a < $outerLoop ; $a++)
{
for ($b = 0; $b < $innerLoop; $b++)
{
if ($badWords[$b] == $inputedText_ToProcess[$a])
{
$badwordCounter = $badWordCounter + 1;
}
}
}
echo "<p>The Total Number of Bad Words Detected In The Text: $badWordCounter</p>";
echo "The entered text string is: $inputedText";
?>
Watch the case of your variables:
$bad**w**ordCounter = $badWordCounter + 1;
btw the nested loops are over-complicated, a cleaner example:
<?php
$_POST['inputText'] = 'come on you ass hole!'; // hardcoded for testing
$badwordCounter = 0;
$badWords = array("bitch", "hoe", "slut", "motherfucker", "fuck", "ass", "cunt");
$inputedText = $_POST['inputText'];
$inputedText_ToProcess = strtolower($inputedText);
$inputedText_ToProcess = explode(" ", $inputedText_ToProcess);
// Iterate through each word
foreach ($inputedText_ToProcess as $word) {
// If that word exists in the badWords array
if (in_array($word, $badWords)) {
$badwordCounter++;
}
}
echo "<p>The Total Number of Bad Words Detected In The Text: $badwordCounter</p>";
echo "The entered text string is: $inputedText";
You can use foreach for going through the whole array. Easier than a 'for' loop. Also, there's a function called in_array, which checks if the string given as the first parameter is in the array given as the second parameter. So this one should work:
<?php
$badWordCounter = 0;
$badWords = array("bitch", "hoe", "slut", "motherfucker", "fuck", "ass", "cunt");
$inputedText = $_POST['inputText'];
$inputedText_ToProcess = explode(" ", $inputedText);
foreach ($inputedText_ToProcess as $value) {
if (in_array(strtolower($value), $badWords)) {
$badWordCounter++;
}
}
echo "<p>The Total Number of Bad Words Detected In The Text: " . $badWordCounter . "</p>";
echo "The entered text string is: " . $inputedText;
?>
You forgot a capital on Word in your variable badWordCounter when incrementing it.
It should read:
$badWordCounter = $badWordCounter + 1;
Instead of:
$badwordCounter = $badWordCounter + 1;
You could have also used $badWordCounter++;
Since everyone is posting additional code that doesn't work, I thought I would offer a solution that is simpler and works with punctuation:
$badWords = array("bitch", "hoe", "slut", "motherfucker", "fuck", "ass", "cunt");
preg_match_all("/".implode('|', $badWords)."/i", $_POST['inputText'], $badWordCounter);
echo '<p>The Total Number of Bad Words Detected In The Text: '.count($badWordCounter[0]).'</p>';
echo 'The entered text string is: '.$_POST['inputText'];
I have been working on this for a while and cannot seem to figure it out at all. Any help would be appreciated. here we go.
I have an html form that has a text box and a submit button. the text entered in the text box is posted to my .php processor form. Once it gets here, I use:
$textdata = $_POST['textdata'];
$input = explode("\n", $textdata);
this takes the data, splits it by line, and stores each line in an array called $input.
from here i can echo $input[0] to get the first line and so on. But I need to use this further down in my script and need to assign a variable to the first line, or $input[0].
$input[0] = $line1; does not work. I think I might have to use extract() and a foreach loop? Any help would be greatly appreciated. thanks!
well fo one thing $input array will always be available, or what you can do if i understand correctly is:
$textdata = $_POST['textdata'];
$input = explode("\n", $textdata); //this should have the array of lines assuming
//that $textdata was \n delimited
$line1 = $input[0]; //use $line1 later in code
$line1 = $input[0];
$line2 = $input[1];
$line3 = $input[2];
// etc.
or:
for ($i=0, $inputlen = count($input); $i < $inputlen; $i++) {
${'line'.($i+1)} = $input[$i];
}
or simply:
list($line1, $line2, $line3) = $input;
$input[0]. $input[0] = $line1;
I can't tell if the full-stop in that line is a full-stop or concatenation operator.
For concatenation, it should be this way.
$input[0] = $input[0] . $line1;
or even shorter
$input[0] .= $line1;
If you're just wanting to assign $input[0] to $line1 by value, it's
$line1 = $input[0];
You can also assign a reference using
$line1 =& $input[0];
Using the latter, any changes to $line1 will be present in $input[0].