I have this array
array (size=7)
0 => string 'Â bs-0468R(20UG)' (length=16)
1 => string 'Â bs-1338R(1ML)' (length=15)
2 => string 'Â bs-1557G(NO BSA)' (length=18)
3 => string 'Â bs-3295R(NO BSA)' (length=18)
4 => string '" bs-0730R' (length=10)
5 => string '" bs-3889R' (length=10)
6 => string 'bs-0919R (NO BSA)' (length=17)
I want to throw away everything and only keep the string that start with bs.
What is the best of doing it ?
Something like this:
$result = array_filter($array, function ($i) {
return strpos($i, 'bs')===0;
});
I love preg_grep:
$result = preg_grep('/^bs/', $array);
I agree with #Casimir et Hippolyte. If you know you're always going to have a controlled dataset such as your example (rare), you can always just reference the string as an array -- which it already is under the hood:
$result = array_filter($array, function ($v) {
return $v[0] . $v[1] == 'bs';
});
Regex is amazing and not a performance problem for most situations, however I have had some issues with it where other functionalities were far faster and efficient when it counted. I understand that statement is not true for the majority of applications, but it is worth noting.
Related
I want to search for '01.' that begins with the number of the array.
Expected output:
1 => string '01.02' (length=5)
2 => string '01.03' (length=5)
3 => string '01.04' (length=5)
32 => string '02.02' (length=5)
33 => string '02.03' (length=5)
34 => string '02.04' (length=5)
35 => string '02.05' (length=5)
My code:
$key = array_search('/^01./', $tomb_datum);
echo $key;
var_dump($key);
These preg match not work.
There is a function dedicated for just this purpose, preg_grep. It will take a regular expression as first parameter, and an array as the second.
See the below example: FIDDLE
$haystack = array (
'01.02',
'01.03',
'02.05',
'02.07'
);
$matches = preg_grep ('/^01/i', $haystack);
print_r ($matches);
If you're looking to filter the array, use array_filter:
$resultArray = array_filter($array, function($elm) {
if (preg_match('/^01/', $elm)) {
return true;
}
return false;
});
Hope this helps.
You could use T-Regx library which allows for all sorts of array filters:
pattern('^01.')->forArray($tomb_datum)->filter()
You can also use other methods like:
filter() - for regular (sequential) arrays
filterAssoc() - for associative arrays (filterAssoc() preserves keys)
filterByKeys() - to filter an array by keys, not values
PS: Notice that with T-Regx you don't need /.?/ delimiters!
In PHP I have an array like this:
array
0 => string 'open' (length=4)
1 => string 'http://www.google.com' (length=21)
2 => string 'blank' (length=5)
but it could also be like:
array
0 => string 'blank' (length=5)
1 => string 'open' (length=4)
2 => string 'http://www.google.com' (length=21)
now it is easy to find "blank" with in_array("blank", $array) but how can I see if one string is starting with "http"?
I've tried with
array_search('http', $array); // not working
array_search('http://www.google.com', $array); // is working
now everything after `http? could vary (how to write vary, varie? could be different is what I mean!)
Now do I need a regex or how can I check if http exists in array string?
Thanks for advices
"Welcome to PHP, there's a function for that."
Try preg_grep
preg_grep("/^http\b/i",$array);
Regex explained:
/^http\b/i
^\ / ^ `- Case insensitive match
| \/ `--- Boundary character
| `------ Literal match of http
`--------- Start of string
Try using the preg_grep function which returns an array of entries that match the pattern.
$array = array("open", "http://www.google.com", "blank");
$search = preg_grep('/http/', $array);
print_r($search);
Solution without regex:
$input = array('open', 'http://www.google.com', 'blank');
$output = array_filter($input, function($item){
return strpos($item, 'http') === 0;
});
Output:
array (size=1)
1 => string 'http://www.google.com' (length=21)
You can use preg_grep
$match = preg_grep("/http/",$array);
if(!empty($match)) echo "http exist in the array of string.";
or you can use foreach and preg_match
foreach($array as $check) {
if (preg_match("/http/", $check))
echo "http exist in the array of string.";
}
I have a string with many caracters and I need obtain data from it.
First of all, I did explode by ';', now I have an array and of each row I have a word into quotes.
I want to remove all, less this word into quotes. I know that is more easy to obtain these words with preg_match, but how is into an array, to save up to go over the array again, I would like to clean it directly with preg_replace.
$array = explode(';', $string);
//36 => string 's:7:"trans_1"' (length=13)
//37 => string 's:3:"104"' (length=9)
//38 => string 's:5:"addup"' (length=11)
//39 => string 's:1:"0"' (length=7)
$array = preg_replace('! !i', '', $array);
I would like to obtain:
//36 => string 'trans_1' (length=6)
//37 => string '104' (length=3)
//38 => string 'addup' (length=5)
//39 => string '0' (length=1)
I tryed differents things, but I can't rid off the letters outside the quotes.
While this isn't a direct answer to your question it solves your problem. The data you are looking at came from the php function serialize() to retrieve the data from that string you need to use the php function unserialize().
$data = unserialize($string);
You could try
preg_replace('!.*"([^"]*)".*!i', '\1', $array);
\1 refers to the first captured group!
This question is different to Split, a string, at every nth position, with PHP, in that I want to split a string like the following:
foo|foo|foo|foo|foo|foo
Into this (every 2nd |):
array (3) {
0 => 'foo|foo',
1 => 'foo|foo',
2 => 'foo|foo'
}
So, basically, I want a function similar to explode() (I really doubt that what I'm asking will be built-in), but which 'explodes' at every nth appearance of a certain string.
How is this possible?
You can use explode + array_chunk + array_map + implode
$string = "foo|foo|foo|foo|foo|foo";
$array = stringSplit($string,"|",2);
var_dump($array);
Output
array
0 => string 'foo|foo' (length=7)
1 => string 'foo|foo' (length=7)
2 => string 'foo|foo' (length=7)
Function used
function stringSplit($string, $search, $chunck) {
return array_map(function($var)use($search){return implode($search, $var); },array_chunk(explode($search, $string),$chunck));
}
I would like a hint or much better a solution for this:
I do a regular expresion match to an url for example '/product/100/'
preg_match('/^\/(?<name>\w+)\/(?<digit>\d+)\/$/', '/product/100/', $matches);
As result of this I get the following array on $matches:
array
0 => string '/product/100/' (length=13)
'name' => string 'product' (length=7)
1 => string 'product' (length=7)
'digit' => string '100' (length=3)
2 => string '100' (length=3)
How can I use reduce this array to get this?
array
'name' => string 'product' (length=7)
'digit' => string '100' (length=3)
After I get the matching expresions, I call a function and give the 'named' keys as arguments to the function.
call_user_func_array($view, $just_named_args_no_integer_keys);
I hope anyone can give me any hint.
Best Regards
Just run the keys you get from array_keys() through array_filter():
/* This is for PHP 5.3, I'm sure you'll figure out how to the same thing pre 5.3 :) */
$allKeys = array_keys($view);
$namedKeys = array_filter($allKeys, function($value) { return !is_numeric($value); });
Update
Did not read the question properly. In this case, actually just foreach over the data:
$namedValues = array();
foreach ($view as $key => $value)
if (!is_numeric($key))
$namedValues[$key] = $value;