Not sure on how to randomize this - php

im trying to randomze a set a results from the database,
this is the bases of the array:
array (size=30)
0 => string '1' (length=1)
1 => string 'jordan' (length=6)
2 => string 'chris' (length=5)
3 => string '1' (length=1)
4 => string '1' (length=1)
5 => string 'card1, card2, card3, card4, card5, card6, card7, card8' (length=54)
6 => string 'card16, card20, card30, card40, card50, card60, card70, card80' (length=62)
7 => string '' (length=0)
8 => string '' (length=0)
9 => string '' (length=0)
10 => string '' (length=0)
11 => string '' (length=0)
12 => string '' (length=0)
13 => string '' (length=0)
14 => string '' (length=0)
15 => string '' (length=0)
16 => string '' (length=0)
17 => string '' (length=0)
18 => string '' (length=0)
19 => string '' (length=0)
20 => string '' (length=0)
21 => string '' (length=0)
22 => string '' (length=0)
23 => string '' (length=0)
24 => string '' (length=0)
25 => string '' (length=0)
26 => string '' (length=0)
27 => string '2013-11-21 04:23:19' (length=19)
28 => string '0' (length=1)
29 => string '0' (length=1)
im wanting to pull the data from array[5] and shuffle it/randomize it
while ($row = mysql_fetch_array($cards, MYSQL_NUM)) {
var_dump($row);
var_dump(array_rand($row[6], 2 ));
}
i've tried various things and now im just at the stage of getting confused even more than i did when i first started can someone help me out?

Explode your string first:
$cards = explode(",", $row[6]);
then, randomize, using shuffle, and implode:
shuffle($cards);
$result = implode(",", $cards);
it should now be a shuffled list.

You can use the php function shuffle in order to randomize in array.
Then you just have to pull the data you want to an array, shuffle it and then display it :)

Try this
function getR($ipt)
{
$pieces = explode(",", $ipt);
return $pieces[mt_rand(0,count($pieces)-1)];
}
while ($row = mysql_fetch_array($cards, MYSQL_NUM))
{
echo getR($row[5]);
}

Related

PHP str_split on string with decoded html_entity

If I run this code:
<?php
$string = 'My string ‘to parse’';
$string_decoded = html_entity_decode($string, ENT_QUOTES, 'utf-8');
$string_array = str_split($string_decoded);
var_dump($string_array);
?>
I get this result:
array (size=28)
0 => string 'M' (length=1)
1 => string 'y' (length=1)
2 => string ' ' (length=1)
3 => string 's' (length=1)
4 => string 't' (length=1)
5 => string 'r' (length=1)
6 => string 'i' (length=1)
7 => string 'n' (length=1)
8 => string 'g' (length=1)
9 => string ' ' (length=1)
10 => string '�' (length=1)
11 => string '�' (length=1)
12 => string '�' (length=1)
13 => string 't' (length=1)
14 => string 'o' (length=1)
15 => string ' ' (length=1)
16 => string 'p' (length=1)
17 => string 'a' (length=1)
18 => string 'r' (length=1)
19 => string 's' (length=1)
20 => string 'e' (length=1)
21 => string '�' (length=1)
22 => string '�' (length=1)
23 => string '�' (length=1)
As you can see, instead of the decoded single quotes (left/right), I'm getting these three characters for each quote...
I noticed that this happens with some entities, but not others. A few that present this issue are ‘ ” $copy;. Some that don't present the same problem are & $gt;.
I tried different charsets but couldn't find one that would work for all.
What am I doing wrong? Is there a way to make it work for all entities? Or at least all the "common" ones?
Thanks.
This should do well:
function mb_str_split($string) {
return preg_split('/(?<!^)(?!$)/u', $string );
}
$string = 'My string ‘to parse’';
$string = utf8_encode($string);
$string_decoded = html_entity_decode($string, ENT_QUOTES, 'utf-8');
$string_array = mb_str_split($string_decoded);
var_dump($string_array);
As mentioned in comments: you need to split the string with mb_split or by regex.
Proof: https://3v4l.org/3FRmG

What array function should I use for creating an index?

Hello guys I am trying to create an index of all words on html page that my crawler parses.
At this moment I have managed to breakdown the html page into an array of words and I have filtered out all the stop words.
At this stage I have a few problems.
The array of words from the parsed html page have words that are repeated, I like that because I still have to record how many times a word appeared in the page.
The array looks like this.
$wordsFromHTML =
array (size=119)
0 => string 'web' (length=3)
1 => string 'giants' (length=6)
2 => string 'vryheid' (length=7)
3 => string 'news' (length=4)
4 => string 'access' (length=6)
5 => string 'mails' (length=5)
6 => string 'mobile' (length=6)
7 => string 'february' (length=8)
8 => string 'access' (length=6)
9 => string 'mails' (length=5)
10 => string 'web' (length=3)
11 => string 'february' (length=8)
12 => string 'access' (length=6)
13 => string 'mails' (length=5)
14 => string 'desktop' (length=7)
15 => string 'february' (length=8)
16 => string 'hosting' (length=7)
17 => string 'web' (length=3)
18 => string 'giants' (length=6)
19 => string 'vryheid' (length=7)
20 => string 'february' (length=8)
22 => string 'us' (length=2)
Now I want to save all the words from the $wordsFromHTML to the $indesArray which is my final index.
It should look like this.
$indexArray = array('web'=>array('url'=>array(0,10,17)))
The problem is how to keep incrementing the position ($wordsFromHTML keys) for each word that was repeated from the $wordsFromHTML array in the final index array.
The index array should only have unique words and if another word that already exists try to come in, we use the already existing word which has the same URL and increment its position.
Hope you understand my question.

How to group array but not create new object?

I have an array that looks like this:
array (size=21)
0 => string '2' (length=1)
1 => string '' (length=0)
2 => string '20' (length=2)
3 => string '19' (length=2)
4 => string '14' (length=2)
5 => string '13' (length=2)
6 => string '' (length=0)
7 => null
8 => null
9 => string '20' (length=2)
10 => null
11 => string '10' (length=2)
12 => string '' (length=0)
13 => null
14 => string '13' (length=2)
15 => null
16 => string '' (length=0)
17 => null
18 => null
19 => string '' (length=0)
20 => string '20' (length=2)
And I would like to create a new array from this array by grouping rows with the same string. e.g.
2 => string '20' (length=2) with 20 => string '20' (length=2) and with 9 => string '20' (length=2)
and
5 => string '13' (length=2) with 5 => string '13' (length=2)
etc.
and order the new created array rows based on how many times the string occures there.
Order need to be DESC from the most occurrences to the last like a classic top something chart (The most present strings are first and the least are low)
So, the modified array will look like this:
array (size=21)
0 => string '20' (length=2)
1 => string '13' (length=2)
...
I also need somehow to handle null results e.g. 17 => null to be not incorporated at all in the final array modified result.
This should do the trick:
// Filter the "null results" first
$myarray = array_filter($myarray, create_function('$arg', '
return !is_null($arg);
'));
$occurrences = array_count_values($myarray);
// EDIT: arsort preserves the key => value correlation
arsort($occurrences, SORT_NUMERIC);
var_dump(array_keys($occurrences));
Try this.
$result = array_count_values($a);
arsort($result);
$result = array_keys($result);

Count() returns different number of entries than var_dump()

Hey guys I'm tired and can't figure this one out so any help would be appreciated.
I have an array called $Row that I get from database table.
When I run var_dump($Row); I get the following:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'erik' (length=4)
'username' => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
'email' => string '' (length=0)
4 => string '0' (length=1)
'date_join' => string '0' (length=1)
5 => string '0' (length=1)
'date_mod' => string '0' (length=1)
6 => string '1' (length=1)
'active' => string '1' (length=1)
7 => string '1' (length=1)
'admin' => string '1' (length=1)
8 => string '0' (length=1)
'deleted' => string '0' (length=1)
When I run echo count($Row); I get value 18.
Count and var_dump are right next to each other, there is no modification of $Row.
Question: Why does Count() return 18 entried when there are only 8 of them inside $Row shown by var_dump()? I guess I just don't understand count()... I checked http://php.net/manual/en/function.count.php, but still don't get it...
Edit: I understand whats wrong know, thanks everyone. Another question. How can I remove the ones that are in a string, for instance I want this kind of a table:
array
0 => string '1' (length=1)
1 => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
4 => string '0' (length=1)
5 => string '0' (length=1)
6 => string '1' (length=1)
7 => string '1' (length=1)
8 => string '0' (length=1)
* I'm using mysql_fetch_array() to retrieve the data and put it into a table.
You are having mixed array integer and associative and having 18 elements. This is I think returned from mysql_fetch_array which by default return associative and numeric indexed array.
Because your strings are also values in $Row they just seem to be indented because of the string char.
That means
'id' => string '1' (length=1)
'username' => string 'erik' (length=4)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
// ...
are also in your array
becuase you're getting back a mix of associative and numeric result set from you db ( you can precise which one you really want with FETCH_ASSOC or alike as a parameter to you dbvendor_query() function ).
There is very well 18 elements in your array.
I think that applying count to this:
0 => string '1' (length=1)
'id' => string '1' (length=1)
returns 2 instead of 1!
0 and id are two different items of the array!
This taken for the others 8 elements make the count return 18
Actually, there are indeed 18 elements in your array:
indices 0 through 8 (9 elements in total)
id, username, ... another 9 elements
9 + 9 = 18
If you only want the numeric indices, and supposing you're using MySQL:
$Row = mysql_fetch_array($result, MYSQL_NUM)
Source: http://php.net/manual/en/function.mysql-fetch-array.php

Get text that is within brackets with single or double quotes

I try to found in my all PHP files the strings inside the i18n functions. Here is an example:
$string = '__("String 2"); __("String 3", __("String 4"));' . "__('String 5'); __('String 6', __('String 7'));";
var_dump(preg_match_all('#__\((\'|")([^\'"]+)(\'|")\)#', $string, $match));
var_dump($match);
I wanna get this result:
array
0 => array
0 => string 'String 2' (length=8)
1 => string 'String 3' (length=8)
2 => string 'String 4' (length=8)
3 => string 'String 5' (length=8)
4 => string 'String 6' (length=8)
4 => string 'String 7' (length=8)
But unfortunately I get this result
array
0 => array
0 => string '__("esto es una prueba")' (length=24)
1 => string '__("esto es una prueba 2")' (length=26)
2 => string '__("prueba 4")' (length=14)
3 => string '__('caca')' (length=10)
4 => string '__('asdsnasdad')' (length=16)
1 => array
0 => string '"' (length=1)
1 => string '"' (length=1)
2 => string '"' (length=1)
3 => string ''' (length=1)
4 => string ''' (length=1)
2 => array
0 => string 'esto es una prueba' (length=18)
1 => string 'esto es una prueba 2' (length=20)
2 => string 'prueba 4' (length=8)
3 => string 'caca' (length=4)
4 => string 'asdsnasdad' (length=10)
3 => array
0 => string '"' (length=1)
1 => string '"' (length=1)
2 => string '"' (length=1)
3 => string ''' (length=1)
4 => string ''' (length=1)
Thanks in advance.
preg_match_all('/(?<=\(["\']).*?(?=[\'"])/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Simple.
Note that I am using ( as an entry point to the match. If you have more exotic input you should provide it.
Don't capture the quotes.
preg_match_all('#__\([\'"]([^\'"]+)[\'"]\)#', $string, $match);
Also take a look at the flags parameter for preg_match_all() for different output formats.

Categories