I am using explode to extract only strings from a whole line. However using this:
$array = explode(" ", $line);
It splits the line by only one space, not by words. For example if $line is
$line="word1 word2 word3"
then I also have spaces among the entries in the array (it contains: "word1", "word2" and "word3", but also one or two " ").
Does anyone know how to obtain only the three entries "word1", "word2" and "word3" in the array ?
Use preg_split , to split on one or more whitespace characters:
$array = preg_split('/\s+/', $line);
Unlike explode (which splits on a string), preg_split splits on a regular expression, so it is more flexible. If you don't need to use a regular expression for your delimiter, you should instead use explode.
Use str_word_count() with a format code or 1 or 2, and a char list indicating that digits should be considered part of the word, because this will also handle splitting against punctuation marks
$array = str_word_count($line, 1, '0123456789');
Personally I would use Tom Fenech's answer but for your existing code:
$array = array_filter(explode(" ", $line));
You can remove empty array values with array filter:
$array = array_filter(explode(" ", $line));
Related
I've filtered some keywords from a string, to remove invalid ones but now I have a lot of extra commas. How do I remove them so I go from this:
,,,,,apples,,,,,,oranges,pears,,kiwis,,
To this
apples,oranges,pears,kiwis
This question is unique because it also deals with commas at the start and end.
$string = preg_replace("/,+/", ",", $string);
basically, you use a regex looking for any bunch of commas and replace those with a single comma.
it's really a very basic regular expression. you should learn about those!
https://regex101.com/ will help with that very much.
Oh, forgot: to remove commas in front or after, use
$string = trim($string, ",");
Use PHP's explode() and array_filter() functions.
Steps:
1) Explode the string with comma.
2) You will get an array.
3) Filter it with array_filter(). This will remove blank elements from array.
4) Again, implode() the resultant array with comma.
5) You will get commas removed from the string.
<?php
$str = ',,,,,apples,,,,,,oranges,pears,,kiwis,,';
$arr = explode(',', $str);
$arr = array_filter($arr);
echo implode(',', $arr);// apples,oranges,pears,kiwis
?>
You can also achieve this by using preg_split and array_filter.
$string = ",,,,,apples,,,,,,oranges,pears,,kiwis,,";
$keywords = preg_split("/[\s,]+/", $string);
$filterd = array_filter($keywords);
echo implode(",",$filterd);
Result:
apples,oranges,pears,kiwis
Explanation:
split with comma "," into an array
use array_filter for removing empty indexes.
implode array with "," and print.
From Manual: preg_split — Split string by a regular expression (PHP 4, PHP 5, PHP 7)
How about using preg_replace and trim?
$str=',,,,,apples,,,,,,oranges,pears,,kiwis,,';
echo trim( preg_replace('#,{2,}#',',',$str), ',');
I am trying to replace some "common" words from a large block of text, however it's only using the last word from the array, please can you see where I'm going wrong?
Thanks
$glue = strtolower ($glue);//make all lower case
//remove common words
$Maffwordlist = array('the','to','for');
foreach($Maffwordlist as $Maffword)
$filtered = preg_replace("/\s". $Maffword ."\s/", " ", $glue);
The extract above only removes 'for' from the text, 'the' and 'to' are still included.
Any help appreciated.
The problem is that the subject of your preg_replace() is always $glue, which itself never changes. Before iterating your list of words, you need to assign the starting contents of $glue into $filtered since that is what you are acting on in order to accumulate all the values into it.
// $filtered is the string you'll be modifying...
$filtered = strtolower ($glue);//make all lower case
$Maffwordlist = array('the','to','for');
foreach($Maffwordlist as $Maffword) {
$filtered = preg_replace("/\s". $Maffword ."\s/", " ", $glue);
}
But we can do better.
A regular expression can be constructed to handle all the replacements without a loop using a (a|b|c) grouping.
// Stick the words together with pipes
$pattern = implode("|", $Maffwordlist);
// And surround with regex delimiters and ()
// so the whole regex looks like /\s(the|to|for)\s/
$pattern = '/\s(' . $pattern . ')\s/';
// And do the operation in one go:
$filtered = preg_replace($pattern, " ", $filtered);
I'll note you may wish to use \b word boundaries instead of \s delimiting these by whitespace. That way, you would get proper replacements in a sentence like "You should not end a sentence with for." where one of your list words appears but not bound by whitespace.
Finally then, you'll end up with multiple consecutive spaces in some places where replacements have taken place. You can collapse those into single spaces with something like the following.
// Replace multiple spaces with a single space
$filtered = preg_replace('/\s+/', ' ', $filtered);
I have to make a regex for two choices, for exemple I have a string:
apps; chrome
I have to split the string in 2 pieces without spaces
1-> apps
2-> chrome
but the problem is that string might be "apps;chrome" (w/o space after ;)
I tried with explode
$part = explode(";", $search);
If the string is with space between characters the second piece have a space.
What I want is a regex for following cases to split them in 2 pieces
apps; chrome
apps;chrome
I hope you understand, sorry for my english :)
The trim function will help:
list($k1,$k2) = array_map("trim",explode(";",$search));
One-liner! =3
Try using trim on the various parts.
e.g.
$parts = array_map('trim', explode(';', $search));
Well, if you are sure about the separators, and you have two options, basically.
1) Using explode(';', $string) and array_map
This will explode the string and them apply trim() over the array;
$slices = explode(';', $string);
$slices_filtered = array_map("trim", $slices);
2) Using preg_split("/[,; \t\n]+/",$string);
This will split strings like "we , are; the \n champions" into {we,are,the,champions}
$slices_filtered = preg_split("/[,; \t\n]+/",$string);
** considering the 'options' won't have spaces on it; if they do, you should use some pattern like
/[,;][ ]*/
Just because you'd specified a Regular Expression ... and this should allow you to match any 2 lower-case alpha strings separated by a semi-colon, with any number (or type) of whitespace "noise".
$sFullString = "app; chrome"; //or wherever you're getting your string from
//RegExp pattern to match many strings including "app;chrome"
$sRegExp = '/^\s*([a-z]+);\s*([a-z]+)\s*$/';
//first replacement
$sAppMatch = preg_replace($sRegExp, "$1", $sFullString);
//second replacement
$sChromeMatch = preg_replace($sRegExp, "$2", $sFillString);
Just use a trim() function before treating your $parts
Why regex?
<?php
$parts = explode(";", $search);
foreach ($parts as $k => $v) {
$parts[$k]=trim($v);
}
I'm having way too much trouble with this simple problem: split a string into an array of 2-character values, i.e.
$string = 'abcdefgh';
// With the correct regex, should return ['ab','cd','ef','gh'];
$array = preg_split("/?????/",$string);
What's the darn regex?
Use str_split() instead.
$chunks = str_split($string, 2);
Hint: If you split ON the characters, you end up with an array of 4 elements that are blank
eg.
/../i
I don't think the preg_split is what you want, perhaps preg_match_all? eg.
$cnt = preg_match_all('/../i', $string, $matches);
I've got a comma delimited string of id's coming in and I need some quick way to split them into an array.
I know that I could hardcode it, but that's just gross and pointless.
I know nothing about regex at all and I can't find a SIMPLE example anywhere on the internet, only huge tutorials trying to teach me how to master regular expressions in 2 hours or something.
fgetcsv is only applicable for a file and str_getcsv is only available in PHP 5.3 and greater.
So, am I going to have to write this by hand or is there something out there that will do it for me?
I would prefer a simple regex solution with a little explanation as to why it does what it does.
$string = "1,3,5,9,11";
$array = explode(',', $string);
See explode()
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter .
Any problem with normal split function?
$array = split(',', 'One,Two,Three');
will give you
Array
(
[0] => One
[1] => Two
[2] => Three
)
If you want to just split on commas:
$values = explode(",", $string);
If you also want to get rid of whitespace around the commas (eg: your string is 1, 3, 5)
$values = preg_split('/\s*,\s*/', $string)
If you want to be able to have commas in your string when surrounded by quotes, (eg: first, "se,cond", third)
$regex = <<<ENDOFREGEX
/ " ( (?:[^"\\\\]++|\\\\.)*+ ) \"
| ' ( (?:[^'\\\\]++|\\\\.)*+ ) \'
| ,+
/x
ENDOFREGEX;
$values = preg_split($regex, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
A simple regular expression should do the trick.
$a_ids = preg_split('%,%', $ids);