I have a string
$style = "font-color:#000;font-weight:bold;background-color:#fff";
I need only
font-color
font-weight
background-color
I have tried
preg_match_all('/(?<names>[a-z\-]+:)/', $style, $matches);
var_dump($matches);
it gives me following output
array
0 =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
'names' =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
1 =>
array
0 => string 'font-color:' (length=11)
1 => string 'font-weight:' (length=12)
2 => string 'background-color:' (length=17)
There are three problems with this output
1. It is two or three dimensional array, I need one dimensional array.
2. It is repeating the information
3. It is appending ":" at the end of each element.
I need a single array like this
array
0 => 'font-color'
1 => 'font-weight'
2 => 'background-color'
Take out the colon:
$style = "font-color:#000;font-weight:bold;background-color:#fff";
preg_match_all('/(?<names>[a-z\-]+):/', $style, $matches);
var_dump($matches['names']);
Then use $matches['names'], since you named it, so you dont have redundant informations
Related
I'm trying to create a simple PHP script that retrieves info from a string and puts it into an array. Ive looked around on some sites on multi capture regex for one pattern but can't seem to get the output im looking for
Currently this is my script.
$input = "username: jack number: 20";
//$input = file_get_contents("test.txt");
preg_match_all("/username: ([^\s]+)|number: ([^\s]+)/", $input, $data);
var_dump($data);
Which produces this output:
0 =>
array (size=2)
0 => string 'username: jack' (length=14)
1 => string 'number: 20' (length=10)
1 =>
array (size=2)
0 => string 'jack' (length=4)
1 => string '' (length=0)
2 =>
array (size=2)
0 => string '' (length=0)
1 => string '20' (length=2)
Im looking to get the data into the form of:
0 =>
array (size=x)
0 => string 'jack'
1 =>
array (size=x)
0 => string '20'
Or two different arrays where the keys correspond to the same user/number combo
You can use match-reset \K:
preg_match_all('/\b(?:username|number):\h*\K\S+/', $input, $data);
print_r($data[0]);
Array
(
[0] => jack
[1] => 20
)
RegEx Breakup:
\b => a word boundary
(?:username|number) => matches username or number. (?:..) is non-capturing group
:\h* => matches a colon followed optional horizontal spaces
\K => match reset, causes regex engine to forget matched data
\S+ => match 1 or more non-space chars
Or else you can use a capturing group to get your matched data like this:
preg_match_all('/\b(?:username|number):\h*(\S+)/', $input, $data);
print_r($data[1]);
Array
(
[0] => jack
[1] => 20
)
(?<=username:|number:)\s*(\S+)
You can use lookbehind here.See demo.
https://regex101.com/r/mG8kZ9/10
I have an array like the example below (from var_dump($tagged);:
array (size=1)
0 =>
array (size=7)
0 => string '#raining' (length=8)
1 => string '#raining' (length=8)
2 => string '#driving' (length=8)
3 => string '#hungrySoon' (length=11)
4 => string '#strangeworld' (length=13)
5 => string '#fruitweekFunky' (length=15)
6 => string '#kevins_rainbow_disco' (length=21)
7 => string '#raining' (length=8)
8 => string '#fruitweekFunky' (length=15)
I am simply after displaying the array as follows (From the most frequent first):
#raining
#fruitweekFunky
#driving ...etc
To achieve exactly what you've asked for (a descending ordered array of the highest occurences to the lowest) step by step:
Count the number of occurences:
$occurences = array_count_values($tagged[0]);
Sort the array (by value, because the number of occurrences is the current array value and the tag is the key - arsort() maintains the original keys):
arsort($occurences);
Get array of the keys for output (because the tags are currently keys):
var_dump(array_keys($occurences));
Fact is, $tagged is an array of arrays. So you need to take its first element.
Try :
$result = array_count_values(array_values($tagged[0]));
var_dump($result);
I have an array with the following values
array (size=7)
0 => string '020120140759' (length=12)
1 => string '020120140759' (length=12)
2 => string '020120140759' (length=12)
3 => string '020220140759' (length=12)
4 => string '020220140759' (length=12)
5 => string '020320140759' (length=12)
6 => string '020320140759' (length=12)
You will notice that the value of certain numbers are the same, I want to extract the last value of each kind that occurs in the array so that a new array will look something likes this.
array (size=2)
2 (this will change to 0 ) => string '020120140759' (length=12)
6 (this will change to 1 ) => string '020320140759' (length=12)
I have tried quite a couple of things but i haven't been successful . Any help would be really appreciated
array_unique is the way to go.
var_dump( array_unique( array_reverse($yourOriginalArrayHere) ) );
you can use array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
the Sorting flags will be
SORT_REGULAR
SORT_NUMERIC
SORT_STRING
SORT_LOCALE_STRING
example
$result = array_unique($input);
print_r($result);
<?
//first step : Exchanges all keys with their associated values in an array $newArray
//second step: get $newArray's key
[array_flip][1]
[array_keys][2]
$array = array(
0 => '020120140759' ,
1 => '020120140759' ,
2 => '020120140759' ,
3 => '020220140759' ,
4 => '020220140759' ,
5 => '020320140759' ,
6 => '020320140759'
);
$newArray = array_keys( array_flip($array) );
echo '<pre>';
var_dump($newArray);
echo '</pre>';
I am trying to turn a URI into key and value pairs in a URI class I have made. The URIs are clean and cannot be parsed into the $_GET variable. The solution has to be within PHP rather than Apache's mod_rewrite.
Consider this URI: /category/music/rows=30/page=12/
The desired key-value pairs I want from this are these:
array('category' <= 'music', 'rows' <= '30', 'page' <= '12')
To try and achieve this I wrote this example code:
$arr = array();
preg_match('/(category\/(?<category>[\w-_]+)|rows=(?<rows>\d+)|page=(?<page>\d+))+/',
"category/music/rows=30/page=12", $arr);
var_dump($arr);
Outputs:
array
0 => string 'category/music' (length=14)
1 => string 'category/music' (length=14)
'category' => string 'music' (length=5)
2 => string 'music' (length=5)
The thinking was that I wanted to match any in a group (/(match this|or this| or this)+/) once or more. I am assuming the problem here is that it matches once and then stops. Also, the parenthesis that group the or statement cause matches that aren't necessary to be stored (the string "category/music" is not required).
I have probably missed something obvious but I can't figure it out. I realize I could run preg_match three times but but it seems like there must be a way to do it in one expression. I also want to keep the code short.
Edit:
preg_match_all works and I have altered the regex to this:
/category/(?[\w-_]+)|rows=(?\d+)|page=(?\d+)/
However, I now get this result:
array
0 =>
array
0 => string 'category/music' (length=14)
1 => string 'rows=30' (length=7)
2 => string 'page=12' (length=7)
'category' =>
array
0 => string 'music' (length=5)
1 => string '' (length=0)
2 => string '' (length=0)
1 =>
array
0 => string 'music' (length=5)
1 => string '' (length=0)
2 => string '' (length=0)
'rows' =>
array
0 => string '' (length=0)
1 => string '30' (length=2)
2 => string '' (length=0)
2 =>
array
0 => string '' (length=0)
1 => string '30' (length=2)
2 => string '' (length=0)
'page' =>
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '12' (length=2)
3 =>
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '12' (length=2)
It would be ideal if it didn't also store "category/music","rows+30","page=12". It has also matched a lot of unnecessary blank strings.
Try using preg_match_all and see if that helps. preg_match stops after it finds the first match.
To get rid of match 1 ('category/music'), try to use a non-capturing group, so do:
(?:category\/(?<category>[\w-_]+)
instead of
(category\/(?<category>[\w-_]+)
take this string as an example: "will see you in London tomorrow and Kent the day after tomorrow".
How would I convert this to an associative array that contains the keywords as keys, whilst preferably missing out the common words, like this:
Array ( [tomorrow] => 2 [London] => 1 [Kent] => 1)
Any help greatly appreciated.
I would say you could :
split the string into an array of words
with explode
or preg_split
depending on the complexity you'll accept for your words separators
use array_filter to only keep the lines (i.e. words) you want
the callback function will have to return false for all non-valid-words
and, then, use array_count_values on the resulting list of words
which will count how many times each words is present in the array of words
EDIT : and, just for fun, here's a quick example :
First of all, the string, that gets exploded into words :
$str = "will see you in London tomorrow and Kent the day after tomorrow";
$words = preg_split('/\s+/', $str, -1, PREG_SPLIT_NO_EMPTY);
var_dump($words);
Which gets you :
array
0 => string 'will' (length=4)
1 => string 'see' (length=3)
2 => string 'you' (length=3)
3 => string 'in' (length=2)
4 => string 'London' (length=6)
5 => string 'tomorrow' (length=8)
6 => string 'and' (length=3)
7 => string 'Kent' (length=4)
8 => string 'the' (length=3)
9 => string 'day' (length=3)
10 => string 'after' (length=5)
11 => string 'tomorrow' (length=8)
Then, the filteting :
function filter_words($word) {
// a pretty simple filter ^^
if (strlen($word) >= 5) {
return true;
} else {
return false;
}
}
$words_filtered = array_filter($words, 'filter_words');
var_dump($words_filtered);
Which outputs :
array
4 => string 'London' (length=6)
5 => string 'tomorrow' (length=8)
10 => string 'after' (length=5)
11 => string 'tomorrow' (length=8)
And, finally, the counting :
$counts = array_count_values($words_filtered);
var_dump($counts);
And the final result :
array
'London' => int 1
'tomorrow' => int 2
'after' => int 1
Now, up to you to build up from here ;-)
Mainly, you'll have to work on :
A better exploding function, that deals with ponctuation (or deal with that during filtering)
An "intelligent" filtering function, that suits your needs better than mine
Have fun !
You could have a table of common words, then go through your string one word at a time, checking if it exists in the table, if not, then add it to your associative array, or +1 to it if it already exists.
using a blacklist of words not to be included
$str = 'will see you in London tomorrow and Kent the day after tomorrow';
$skip_words = array( 'in', 'the', 'will', 'see', 'and', 'day', 'you', 'after' );
// get words in sentence that aren't to be skipped and count their values
$words = array_count_values( array_diff( explode( ' ', $str ), $skip_words ) );
print_r( $words );