I have a function:
Function returns numbers from string line.
function get_numerics ($str) {
preg_match_all('/\d+/', $str, $matches);
return $matches[0];
}
And I need to get numbers to an array in my php file.
How to do that?
$counter = $user_count[$sk]; //$user_count[$sk] gives me a string line
//$user_count[$sk] is "15,16,18,19,18,17" - And i need those numbers seperated to an array
$skarray[] = get_numerics($counter); //Something is wrong?
Explode could work, but $user_count[$sk] line could be "15, 16, 19, 14,16"; ie it may or may not contain spaces.
You don't need regex for this, explode() combined with str_replace() will do it:-
$user_count = "15 ,16,18 ,19,18, 17";
$numbers = explode(',', str_replace(' ', '', $user_count));
var_dump($numbers);
Output:-
array (size=6)
0 => string '15' (length=2)
1 => string '16' (length=2)
2 => string '18' (length=2)
3 => string '19' (length=2)
4 => string '18' (length=2)
5 => string '17' (length=2)
if you have a string that looks like:
$str = "15,16,17,18,19";
And want to split them into an array, you can use explode
$arr = explode(",", $str);
see http://www.php.net/manual/en/function.explode.php
Related
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!
I have an array like this:
$names = array('John Doe','Marc','Rudolph', 'John Doe','Steve', 'Marc');
How can i remove the duplicate names with PHP?
The new array must be:
$names = array('John Doe','Marc','Rudolph','Steve');
What is already the function to do this.?
Always check the php.net manual first.
$names = array('John Doe','Marc','Rudolph', 'John Doe','Steve', 'Marc');
$names = array_unique($names);
http://php.net/manual/en/function.array-unique.php
Use array_unique():
$myArray = array(....);
$uniqueArray = array_unique( $myArray );
See docs: http://php.net/manual/en/function.array-unique.php
You can use array_unique But please note that this would not work for case sensitive array such as
$names = array('John Doe','Marc','Rudolph','John doe','Steve','Marc');
^-- Capital ^--Small
If you run
var_dump(array_unique($names));
Output
array
0 => string 'John Doe' (length=8)
1 => string 'Marc' (length=4)
2 => string 'Rudolph' (length=7)
3 => string 'John doe' (length=8)
4 => string 'Steve' (length=5
Solution Convert everything to the same case before you check if it unique
var_dump(arrayUnique($names));
Output
array
0 => string 'John Doe' (length=8)
1 => string 'Marc' (length=4)
2 => string 'Rudolph' (length=7)
4 => string 'Steve' (length=5)
Function Used
function arrayUnique($array) {
return array_intersect_key($array, array_unique(array_map("strtolower", $array)));
}
If have an array like this:
array
0 => string '62 52, 53' (length=9)
1 => string '54' (length=2)
It's from user input, and you never know how/what they enter ;)
What I want in the end is this:
array
0 => string '62' (length=2)
1 => string '52' (length=2)
2 => string '53' (length=2)
3 => string '54' (length=2)
Here's how I do it:
$string = implode(',', $array);
$string = str_replace(', ', ',', $string);
$string = str_replace(' ', ',', $string);
$array = explode(',', $string);
Seems really clunky. Is there a more elegant way? One that maybe has better performance?
On each string:
preg_match_all("/[ ,]*(\d+)[ ,]*/", $list, $matches);
Then read $matches[1] for the numbers
Not sure about performance but you can use a regex to grab only numbers after you join everything into a string.
$string = implode(' ', $array);
preg_match_all('/\d+/', $string, $matches);
print_r($matches[0]);
You may want to use preg_split and array_merge (PHP 4, PHP 5)
I have a string:
$string = "R 124 This is my message";
At times, the string may change, such as:
$string = "R 1345255 This is another message";
Using PHP, what's the best way to remove the first two "words" (e.g., the initial "R" and then the subsequent numbers)?
Thanks for the help!
$string = explode (' ', $string, 3);
$string = $string[2];
Must be much faster than regexes.
One way would be to explode the string in "words", using explode or preg_split (depending on the complexity of the words separators : are they always one space ? )
For instance :
$string = "R 124 This is my message";
$words = explode(' ', $string);
var_dump($words);
You'd get an array like this one :
array
0 => string 'R' (length=1)
1 => string '124' (length=3)
2 => string 'This' (length=4)
3 => string 'is' (length=2)
4 => string 'my' (length=2)
5 => string 'message' (length=7)
Then, with array_slice, you keep only the words you want (not the first two ones) :
$to_keep = array_slice($words, 2);
var_dump($to_keep);
Which gives :
array
0 => string 'This' (length=4)
1 => string 'is' (length=2)
2 => string 'my' (length=2)
3 => string 'message' (length=7)
And, finally, you put the pieces together :
$final_string = implode(' ', $to_keep);
var_dump($final_string);
Which gives...
string 'This is my message' (length=18)
And, if necessary, it allows you to do couple of manipulations on the words before joining them back together :-)
Actually, this is the reason why you might choose that solution, which is a bit longer that using only explode and/or preg_split ^^
try
$result = preg_replace('/^R \\d+ /', '', $string, 1);
or (if you want your spaces to be written in a more visible style)
$result = preg_replace('/^R\\x20\\d+\\x20/', '', $string, 1);
$string = preg_replace("/^\\w+\\s\\d+\\s(.*)/", '$1', $string);
$string = preg_replace('/^R\s+\d+\s*/', '', $string);