<?php
ini_set('error_reporting', '-1');
ini_set('display_errors', '1');
ini_set('apc.enabled', '0');
gc_enable();
$array = array("php", "php_php", "php_php", "php_php", "php");
$arraysize = count($array);
$style = " style='border: 1px solid black;'";
$strcmpcharcount = 0;
$equalcmpcharcount = 0;
foreach ($array as $key)
{
$strcmpcharcount = 0;
$equalcmpcharcount = 0;
if (strstr($key, "_") !== false)
{
$strstr[] = "found";
$explodedstring1[] = explode("_", $key);
}
else
{
$strstr[] = "not found";
$explodedstring1[] = "not found";
}
if (strpos($key, "_") !== false)
{
$strpos[] = "found";
$explodedstring2[] = explode("_", $key);
}
else
{
$strpos[] = "not found";
$explodedstring2[] = "not found";
}
if (preg_match("/[^_+$]/", $key))
{
$preg_match[] = "found";
$explodedstring3[] = explode("_", $key);
}
else
{
$preg_match[] = "not found";
$explodedstring3[] = "not found";
}
$keysize = strlen($key);
for ($i = 0; $i < $keysize; $i++)
{
if (strcmp($key[$i], "_") === 0) { $strcmpcharcount++; }
}
for ($j = 0; $j < $keysize; $j++)
{
if ($key[$j] === "_") { $equalcmpcharcount++; }
}
if ($strcmpcharcount > 0)
{
$strcmp[] = "found";
$explodedstring4[] = explode("_", $key);
}
else
{
$strcmp[] = "not found";
$explodedstring4[] = "not found";
}
if ($equalcmpcharcount > 0)
{
$equalcmp[] = "found";
$explodedstring5[] = explode("_", $key);
}
else
{
$equalcmp[] = "not found";
$explodedstring5[] = "not found";
}
}
echo "<table$style>
<th$style>
<tr>
<td$style>strstr()</td>
<td$style>strpos()</td>
<td$style>preg_match()</td>
<td$style>strcmp()</td>
<td$style>'==='</td>
</tr>
</th>";
for($k = 0; $k < $arraysize; $k++)
{
echo "<tr>
<td$style>$strstr[$k]</td>
<td$style>$strpos[$k]</td>
<td$style>$preg_match[$k]</td>
<td$style>$strcmp[$k]</td>
<td$style>$equalcmp[$k]</td>
</tr>";
}
echo "</table>";
exit();
?>
The problem is with first two functions - they randomly fails to find the underscore char. In fact I called more than 50 times the script to got proper results. Added and preg_match() test but just to know I'm not sure if it has valid regex.
You're adding new elements to $strstr, $strpos tables and so on and in the end you do for... and printing from these tables where $k keys don't necessarily have to exists.
Check var_dump or print_r on these tables and you will see that in fact they got elements, but their indexes aren't matched with $array indexes (and i guess that's what you want to achieve).
You can change foreach ($array as $key) to foreach ($array as $index => $key) and all occurences like $strstr[] = "found"; to $strstr[$index] = "found"; (also for other recognision methods) and then run the script again to see results.
In last block (for($k = 0; $k < $arraysize; $k++)...) you should either validate if $strstr[$k] (and other arrays) exists before printing it, or print these arrays separately by foreach.
You can also make one table for results and make it multidimensional with function names as keys in the first level and put results in there.
Related
How can I achieve php char count with php :
input : bbbvvvvzzxxc
output : 3b4v2z2xc
I try this code :
$str = "aabbbccaaaac";
$arr1 = str_split($str);
$count=1;
foreach($arr1 as $key=>$char) {
if($key==0){
$pre=$char;
}
if($pre==$char){
$count++;
} else{
$count=1;
$pre=$char;
}
echo $pre.'---'.$char.'__';
if($pre != $char){
echo $char. $count ;
} else {
// echo $char;
}
}
but not working :
its simple to set count of character
$str = "aabbbccaaaac";
$arr1 = str_split($str);
$count=0;
$pre = '';
$result = ''; // our result string
foreach($arr1 as $char){
if($char == $pre){
$count++;
}else{
if($count > 0){ // To check whether it was the first iteration
$result .= $count.$pre;
}
$count = 1;
$pre = $char;
}
}
$result .= $count.$pre; // For last iteration
echo $result;
I'm trying to place a value and retrieve it later on
$fruits = array("Watermelon", "Lime", "Lemon");
$result = count($fruits);
$i = 0;
while ($i < $result) {
echo "<br>" . "<input type='checkbox' name='fruit[]' value='$fruits[$i]'>";
echo "$fruits[$i]";
$i++;
}
The first set I assigned the fruit to the value per the array, the code below is supposed to set the values in the if statement and compare them to the checkbox. If I put in the fruit by name it works e.g. "Watermelon" works but "$fruits[0]" does not.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST["fruit"])){
$i = 0;
while($i < $result) {
if(in_array('$fruits[$i]', $_POST['fruit'])){
echo "It works!";
$i++;
} else {
$i++;
}
}
} else {
echo "choose a box";
$i = $result;
}
You can do this easily with foreach()(if you want to).
if(isset($_POST['fruit'])) {
foreach($_POST['fruit'] as $fruit) {
$fruits[] = $fruit;
}
if(!empty($fruits)) {
echo 'It Works.!';
} else {
echo "choose a box";
}
}
You use $i++ in work condition so if will increase $i to 1 until last index of $fruits ,if $fruits last index is not exist in $_POST it will nothing display.. use exit can be display.
if(isset($_POST["fruit"])){
$i = 0;
while($i < $result) {
if(in_array($fruits[$i],$_POST['fruit'])){
echo "It works!";
exit;
} else {
$i++;
}
}
}
Also no need to quote in your array...
if(in_array('$fruits[$i]', $_POST['fruit'])) //remove ''
function PigLatin($sentence)
{
$vowelSufix = "way";
$consonantSufix = "ay";
$vowelArray = array('a','e','o','u','i');
$finalword;
$wordArray = explode(' ', $sentence);
foreach ($wordArray as $value)
{
$word = $value;
$consonant = $word[0];
if (in_array($word[0], $vowelArray))
{
$finalword = substr($word, 1). $word[0]. $vowelSufix. "<br />";
}
else
{
for ($i=1; $i <strlen($word) ; $i++)
{
if (in_array($word[$i], $vowelArray))
{
$finalword = substr($word, $i). $consonant. $consonantSufix . "<br />";
}
else
{
$consonant .= $word[$i];
}
}
}
if ($finalword[0] == $finalword[1])
{
return substr($finalword, 1);
}
$finalword .= $finalword;
}
var_dump($wordArray);
}
So basicly it is giveing me the follow errors "Uninitialized string offset".I know this error comes because i am useing the arrays not proberly but i am stuck, Can someone please help me?
Your script doesn't handle the case where $word is empty, which will happen if you have two spaces in a row in the sentence. If $word is an empty string, $word[0] will get the error you reported, because there is no such character in the string.
Change the loop to:
foreach ($wordArray as $word)
{
if ($word === '') {
continue;
}
This will skip empty words. Note also that you don't need separate variables $value and $word.
I'm using this script to list a few Twitch.tv streams and their status (offline or online).
If there are no online streams found, I want it to display a text saying that all are offline.
Code that checks if the added streams are online:
//get's member names from stream url's and checks for online members
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
//checks if player streams are online
function onlinecheck($online, $viewers)
{
//If the variable online is not equal to null, there is a good change this person is currently streaming
if ($online != null)
{
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
Full code:
<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php
$members = array("ncl_tv");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
function onlinecheck($online, $viewers) {
if ($online != null) {
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
$alloffline = "All female user streams are currently offline.";
function signin($person){
if($person != null){
return $person;
}
?>
</body>
</html>
............................................................................................................................................................................
Is it because your $userGrab URL contains usernames twice? This is the URL whose contents you're retrieving:
http://api.justin.tv/api/stream/list.json?channel=painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv,painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv
Having looked at the response, it doesn't look like it's causing the problem. The strange URL is a result of you appending to the $userGrab string in the first foreach loop, after you've already added them with the implode() function call before. I think twitch.tv is rightly ignoring duplicate channels.
If all the values in $checkedOnline are null, everyone is offline. Put this at the end of your first code sample:
$personOnline = false;
foreach($checkedOnline as $person) {
if($person !== null) {
$personOnline = true;
break;
}
}
if(!$personOnline) {
echo 'No one is online';
}
else {
//there is at least someone online
}
So I have this code below but I am wondering how I can tell if '$text' contains the words 'by owner'. How do I do this? I looked around but I can't find anything.
foreach($anchors as $a) {
$i = $i + 1;
$text = $a->nodeValue;
$href = $a->getAttribute('href');
if ($i > 22 && $i < 64 && ($i % 2) == 0) {
//if ($i<80) {
echo "<a href =' ".$href." '>".$text."</a><br/>";
}
// }
//$str = file_get_contents($href);
//$result = (substr_count(strip_tags($str),"ipod"));
//echo ($result);
}
Something like:
$text = $a->nodeValue;
if(strpos($text, "by owner") == -1){ // if you want the text to *start* with "by owner", you can replace this with strpos($text, "by owner") != 0
echo "Doesn't have by owner";
}
else{
echo "Has by owner";
}