I am having input as Word "CODE"
and I want to get output as "CCOCODCODE"
Please help me with the logic to print this output.
Thanks.
It's rather simple. Looking at your pattern, you need to concatenate all possible prefixes of CODE one after the other. So, maintain a result variable and keep concatenating substrings with 0 as the start point and end point being each index in the string.
<?php
$str = 'CODE';
$result = '';
for($i=0;$i<strlen($str);++$i){
$result .= substr($str,0,$i+1);
}
echo $result;
Try to avoid loops whenever you can.
Short and simple does the trick :-)
<?PHP
$Text = 'CODE';
echo implode(array_map(function($Position) use ($Text) { return substr($Text,0,$Position+1); },array_keys(str_split($Text))));
Related
I'm trying to replace the value from array.
$row= array("id"=>"35", "name"=>"test","first_name"=>"noor","last_name"=>"fathima");
// Eval Statement -
$row = $row['first_name'].' '.$row['last_name'];
eval("\$row = \"$row\";");
//This should return noor fathima
I am unable to replace the values. Can anyone please help me out?
Not a huge fan of eval() as it can be a pain, but the main thing is to get all of the right $'s and quotes escaped/unescaped etc. To get round this I've put the expression into single quotes as this stops any interpretation - until you eval() it...
$row= array("id"=>"35", "name"=>"test","first_name"=>"noor","last_name"=>"fathima");
eval('$result = $row["first_name"]." ".$row["last_name"];');
echo $result;
gives...
noor fathima
Why you need eval() here because it is already returning what you want as output?
<?php
$row= array("id"=>"35", "name"=>"test","first_name"=>"noor","last_name"=>"fathima");
echo $row['first_name'].' '.$row['last_name'];
?>
DEMO: https://3v4l.org/7KTH0
I have developed this small code to check if 2 text, one from a database and the other from an outside imput have common words.
The problem is that I get a message "Argument is not an array".
I cannot see where is the problem.
I also need to check if the 2 messages if should have the same words are in the same sequence.
Please help to understand where is the mistake.
Thanks
$checkMsg=strip_tags($_POST['checkMsg']); // message from input form
$message // message from database
$MsgWords = preg_split("/[\s,]+/", $checkMsg);
if(!empty($checkMsg)){
foreach ($MsgWords as $Neword)
{ $Neword = trim($Neword);
echo " $Neword";
}
$word = preg_split("/[\s,]+/", $message);
foreach ($word as $currentWord)
{
$currentWord = trim($currentWord);
echo " $currentWord";
}
$intersect=array_intersect( $Neword ,
$currentWord);
echo" Your common words are: $intersect";}else{echo "No common words";}
As others have said you're comparing the strings not the array. Your code should be something like this (you'll have to probably change this a little its just an example)
$checkMsg=strip_tags($_POST['checkMsg']); // message from input form
$message // message from database
$MsgWords = preg_split("/[\s,]+/", $checkMsg);
if(!empty($checkMsg)){
$intersect=array_intersect($message,$MsgWords);
if (count($intersect)>1) {
//only show output if there are matches
echo "Words in common are:<br />";
foreach ($intersect as $Neword) {
$Neword = trim($Neword);
echo $Neword."<br />";
}
} else {
echo "There are no words in common";
}
}
Okay, so firstly you're looping through the two arrays and changing the value, but the way you have it, you are just changing a temp copy of the value, not the value in the array. To do that, you need to use the & sign in the foreach() to tell it to use a reference variable in the loop, like this:
foreach ($MsgWords as &$Neword) { //added the & sign here.
$Neword = trim($Neword);
}
Do the same thing to the other foreach() loop too.
Secondly, your array_intersect() call is looking at the single strings, not at the whole arrays. You need it to look at the arrays:
//your incorrect version:
$intersect=array_intersect( $Neword, $currentWord);
//corrected version, using your variable names.
$intersect=array_intersect( $MsgWords, $word);
That should solve your problems.
[EDIT]
Also, note the array_intersect() outputs an array (ie the array of the intersection between the two input arrays). You can't use echo() to print an array directly. If you try, it will just show the word 'Array'. You need to convert it into a string first:
//your incorrect code.
echo "Your common words are: $intersect";
//corrected code:
echo "Your common words are: ".implode(',',$intersect);
I would also note that your coding style is very messy and hard to read. I strongly recommend trying to tidy it up; follow some kind of indentation and variable naming rules. Otherwise it will be very hard to maintain.
The title may be a little confusing. This is my problem:
I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:
$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;
That would print: "hello". Now, I've got a problem with this:
$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;
That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?
Thanks in advance.
For the moment, I could only think of something like this:
<?php
// literal are simple
$literal = "Hello";
$vv = "literal";
echo $$vv . "\n";
// prints "Hello"
// for containers it's not so simple anymore
$container = array("Hello" => "World");
$vv = "container";
$reniatnoc = $$vv;
echo $reniatnoc["Hello"] . "\n";
// prints "World"
?>
The problem here is that (quoting from php: access array value on the fly):
the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.
Would PHP allow the subscript notation anywhere, one could write this more dense as
echo $$vv["Hello"]
Side note: I guess using variable variables isn't that sane to use in production.
How about this? (NOTE: variable variables are as bad as goto)
$variablename = 'array';
$key = 'index';
echo $$variablename[$key];
I have this:
echo 'm=>'.$fin_type_2; // echoes 'test text';
Now, if I do the following, I get nothing, whereas I was expecting the same result as above. What I am doing wrong here ?
$z=2;
echo 'm=>'.$fin_type_{$z}; // echoes nothing but m=>
Help appreciated.
Many thanks.
i would use something like this:
${'fin_type_'.$z}
(besides this page of php documentation helped myself sometimes)
You should probably use arrays (numeric and associative) to achieve your goal.
I'm afraid that closest you can get is
<?php
$fin_type_2 = 'test text';
$z = 2;
$v = "fin_type_$z";
echo 'm=>'.$v;
However, think twice before applying variable variables like that. Perhaps using array would fit your case better?
<?php
$fin_type = array();
$fin_type[2] = 'test text';
$z = 2;
echo "m=>{$fin_type[$z]}";
please see: http://pastebin.com/5za3uCi1
I'm quite new to php and I'm editing the ventrilo status script. What I'd like it to do is that it stores everything in one big variable for easy parsing instead of using separate echo's. Can someone tell me how I can accomplish this?
Thanks,
Dennis
You can use the output buffer and get the contents of it:
ob_start();
echo 'foobar';
$contents = ob_get_contents(); // now contains 'foobar'
ob_end_clean();
declare a variable at the beginning, say $data or whatever. then, replace the echo calls:
echo "hello";
with this:
$data .= "hello";
then return the $data variable at the end of the function.
Instead of the echo, you can use a simple affectation :
$request = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
But you'll soon have issues to manage multiple variables.
You could create an object to handle and store your information, but If you need something easy to set up and simple to operable, I'd go for arrays :
$ventriloStatus = array();
$ventriloStatus['requestObj'] = $stat->Request();
$ventriloStatus['requestMsg'] = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
Add your data using keys.
Then retrieve the value easily :
echo $ventriloStatus['requestMsg'];
You can even parse your data using a simple loop
foreach($ventriloStatus as $key => $value){
echo $key.' : '.$value.'<br />';