I looked up splitting the string into array in google.I have found that str_split is working.By explode it's doesn't work in below condition.How can I split the string by explode()?
<?php
$string = "EEEE";
print_r(str_split($string));//Array ( [0] => E [1] => E [2] => E [3] => E )
print_r(explode("",$string));//Empty delimiter error
?>
As indicated by your error, explode requires a delimiter to split the string!
You should try,
$str = "EEEE";
$answer = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
alternative way would be preg_split.
Related
I need to split my string input into an semicolon separate as below.
Original String: Loganathan <logu#gmail.com>; Nathan <nathan#gmail.com>; Tester <tester#gmail.com>;
I need split like
Loganathan, logu#gmail.com
Nathan, nathan#gmail.com
Tester, tester#gmail.com
How can I go about accomplishing this?
You can use explode function. explode link
$str = "Loganathan <logu#gmail.com>; Nathan <nathan#gmail.com>; Tester <tester#gmail.com>;";
$str = str_replace(array(" <",">"),array(", ",""),$str);
$converted = explode(";",$str);
print_r($converted);
Which gives you output like
Array(
[0] => Loganathan, logu#gmail.com
[1] => Nathan, nathan#gmail.com
[2] => Tester, tester#gmail.com
)
Use explode
$str = 'Loganathan <logu#gmail.com>; Nathan <nathan#gmail.com>; Tester <tester#gmail.com>;';
//Removing the "<>" symbols
$str = str_replace("<",",",$str);
$str = str_replace(">","",$str);
$result = explode(";", $str);
print_r(array_filter($result)); //Removing empty array values
Result:
Array
(
[0] => Loganathan ,logu#gmail.com
[1] => Nathan ,nathan#gmail.com
[2] => Tester ,tester#gmail.com
)
For the following code:
$string = "hello: Mister, Winterbottom";
$words = preg_split("/[\s,]+/", $string);
print_r ($words);
I get:
Array ( [0] => hello: [1] => Mister [2] => Winterbottom )
but I want the results to be:
Array ( [0] => hello [1] => Mister [2] => Winterbottom )
so that it will ignore the colon. How can I do it?
If you need to expand your character class with :, just put it inside it and use
/[\s,:]+/
See its demo here. Or, just use /\W+/ to split with 1+ non-word characters.
$words = preg_split("/[\s,:]+/", $string);
print_r ($words);
// Or
print_r(preg_split("/\W+/", $string));
See the PHP demo
$string = "hello: Mister, Winterbottom";
$words = preg_split("/[\s,]+/", $string);
$words[0] = rtrim($words[0],":");
print_r ($words);
I am trying to explode / preg_split a string so that I get an array of all the values that are enclosed in ( ). I've tried the following code but I always get an empty array, I have tried many things but I cant seem to do it right
Could anyone spot what am I missing to get my desired output?
$pattern = "/^\(.*\)$/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
Current output Array ( [0] => [1] => )
Desired output Array ( [0] => "(y3,x3)," [1] => "(r4,t4)" )
With preg_split() your regex should be matching the delimiters within the string to split the string into an array. Your regex is currently matching the values, and for that, you can use preg_match_all(), like so:
$pattern = "/\(.*?\)/";
$string = "(y3,x3),(r4,t4)";
preg_match_all($pattern, $string, $output);
print_r($output[0]);
This outputs:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
)
If you want to use preg_split(), you would want to match the , between ),(, but without consuming the parenthesis, like so:
$pattern = "/(?<=\)),(?=\()/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
This uses a positive lookbehind and positive lookahead to find the , between the two parenthesis groups, and split on them. It also output the same as the above.
You can use a simple regex like \B,\B to split the string and improve the performance by avoiding lookahead or lookbehind regex.
\B is a non-word boundary so it will match only the , between ) and (
Here is a working example:
http://regex101.com/r/cV7bO7/1
$pattern = "/\B,\B/";
$string = "(y3,x3),(r4,t4),(r5,t5)";
$result = preg_split($pattern, $string);
$result will contain:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
[2] => (r5,t5)
)
for example i have sentenes like this:
$text = "word, word w.d. word!..";
I need array like this
Array
(
[0] => word
[1] => word
[2] => w.d
[3] => word".
)
I am very new for regular expression..
Here is what I tried:
function divide_a_sentence_into_words($text){
return preg_split('/(?<=[\s])(?<!f\s)\s+/ix', $text, -1, PREG_SPLIT_NO_EMPTY);
}
this
$text = "word word, w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
works, but i have second question i want to write list in mu regular exppression
"w.d" is special case.. for example this words is my list "w.d" , "mr.", "dr."
if i will take text:
$text = "word, dr. word w.d. word!..";
i need array:
Array (
[0] => word
[1] => dr.
[2] => word
[3] => w.d
[4] => word
)
sorry for bad english...
Using preg_split with a regex of /[^\w]*([\s]+[^\w]*|$)/ should work fine:
<?php
$text = "word word w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
?>
DEMO
Output:
Array
(
[0] => word
[1] => word
[2] => w.d
[3] => word
)
Use the function explode, that will split the string into an array
$words = explode(" ", $text);
use
str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
see here http://php.net/manual/en/function.str-word-count.php
it does exactly what you want. So in your case :
$myarray = str_word_count ($text,1);
From this string:
$input = "Some terms with spaces between";
how can I produce this array?
$output = ['Some', 'terms', 'with', 'spaces', 'between'];
You could use explode, split or preg_split.
explode uses a fixed string:
$parts = explode(' ', $string);
while split and preg_split use a regular expression:
$parts = split(' +', $string);
$parts = preg_split('/ +/', $string);
An example where the regular expression based splitting is useful:
$string = 'foo bar'; // multiple spaces
var_dump(explode(' ', $string));
var_dump(split(' +', $string));
var_dump(preg_split('/ +/', $string));
$parts = explode(" ", $str);
print_r(str_word_count("this is a sentence", 1));
Results in:
Array ( [0] => this [1] => is [2] => a [3] => sentence )
Just thought that it'd be worth mentioning that the regular expression Gumbo posted—although it will more than likely suffice for most—may not catch all cases of white-space. An example: Using the regular expression in the approved answer on the string below:
$sentence = "Hello my name is peter string splitter";
Provided me with the following output through print_r:
Array
(
[0] => Hello
[1] => my
[2] => name
[3] => is
[4] => peter
[5] => string
[6] => splitter
)
Where as, when using the following regular expression:
preg_split('/\s+/', $sentence);
Provided me with the following (desired) output:
Array
(
[0] => Hello
[1] => my
[2] => name
[3] => is
[4] => peter
[5] => string
[6] => splitter
)
Hope it helps anyone stuck at a similar hurdle and is confused as to why.
Just a question, but are you trying to make json out of the data? If so, then you might consider something like this:
return json_encode(explode(' ', $inputString));