I have a dialogue file that looks like so:
CHARACTER MOOD PROMPT RESPONSE TEXT LEVEL PATH
As you can see, everything is separated by spaces. The trick comes in when
PROMPT RESPONSE TEXT is supposed to be one header (read together) all heading groups are separated by no more than two spaces while each heading is separated by 3+ spaces. What I am trying to do is take this line and add it into an array much like this:
array(4) => {
[0]=> string(9) "CHARACTER",
[1]=> string(4) "MOOD",
[2]=> string(21) "PROMPT RESPONSE TEXT",
[3]=> string(5) "LEVEL",
[4]=> string(4) "PATH"
}
I am trying to use preg_split with the following regexp /\s\s\s+/ but it does nothing more than yield an empty array. I assume that the regexp would split if on any amount of spaces equal to or greater than 3. Is there something more to this?
You can use the following, this looks for whitespace ( at least 3 times )
$results = preg_split('/\s{3,}/', $text);
var_dump($results);
Output
array(5) {
[0]=> string(9) "CHARACTER"
[1]=> string(4) "MOOD"
[2]=> string(21) "PROMPT RESPONSE TEXT"
[3]=> string(5) "LEVEL"
[4]=> string(4) "PATH"
}
If you dont want to have the overhead of loading the regex engine you could use this
<?php
$t = 'CHARACTER MOOD PROMPT RESPONSE TEXT LEVEL PATH';
$u = explode(' ',$t);
print_r($u);
$new_u = array();
foreach( $u as $key => $val) {
if ($val != '') {
$new_u[] = trim($val);
}
}
print_r($new_u);
Results
Array
(
[0] => CHARACTER
[1] =>
[2] => MOOD
[3] =>
[4] =>
[5] => PROMPT RESPONSE TEXT
[6] =>
[7] => LEVEL
[8] =>
[9] =>
[10] => PATH
)
Array
(
[0] => CHARACTER
[1] => MOOD
[2] => PROMPT RESPONSE TEXT
[3] => LEVEL
[4] => PATH
)
Related
My array looks like:
{flower},{animals},{food},{people},{trees}
I want to explode with {, , & }.
My output should contain only words inside curly brackets.
My code:
$array = explode("},{", $list);
After execution of this code $array will be
$array = Array (
[0] => {flower
[1] => animals
[2] => food
[3] => people
[4] => trees}
)
But output array should be:
$array = Array (
[0] => flower
[1] => animals
[2] => food
[3] => people
[4] => trees
)
Can anyone please tell me how can I modify my code to get this array?
I would go for preg_split like below
<?php
$list = "{flower},{animals},{food},{people},{trees}";
$array = preg_split('/[},{]/', $list, 0, PREG_SPLIT_NO_EMPTY);
print_r($array);
?>
The output is
Array
(
[0] => flower
[1] => animals
[2] => food
[3] => people
[4] => trees
)
You could try to extract the words using a RegEx instead of splitting the string:
$list = "{flower},{animals},{food},{people},{trees}";
// Match anything between curly brackets
// The "U" flag prevents the regex to make a single match with the first and last brackets
preg_match_all('~{(.+)}~U', $list, $result);
// Only keep the 1st capturing group
$words = $result[1];
var_dump($words);
Output:
array(5) {
[0]=>
string(6) "flower"
[1]=>
string(7) "animals"
[2]=>
string(4) "food"
[3]=>
string(6) "people"
[4]=>
string(5) "trees"
}
Hello I have this string
$chineseString = "号码:91"
What I want to do is to explode() it and get a result like this:
array:2[
[0] => "号码",
[1] => "91"
]
The reason explode() didn't work for you is that your chineseString variable contains what is called in unicode a FULLWIDTH COLON (U+FF1A) and you are trying to split by a different character, a COLON (U+003A). So, if you use the correct character it will work.
$chineseString = "号码:91";
print_r(explode(":", $chineseString ));
Outputs: Array([0] => 号码, [1] => 91)
Take a look at this http://codepad.org/yYO3nljF
<?php
$chineseString = "号码:91";
$d = explode(":",$chineseString );
var_dump($d);
?>
output
array(2) {
[0]=>
string(6) "号码"
[1]=>
string(2) "91"
}
This seems to work for me:
$chineseString = "号码:91";
print_r(preg_split('/:/', $chineseString));
Results in: Array ( [0] => 号码 [1] => 91 )
I have a url parameter named data that contains a comma separated string with some enclosed in double quotes like this:
localhost/index.php?data=val1,val2,val3,"val4","val5",val6
I am trying to parse the string and put it into an array. Using str_getcsv($_GET['data'],',','"'); gives me the output like this:
Array
(
[0] => val1
[1] => val2
[2] => val3
[3] =>
)
I would like the array to look like this:
Array
(
[0] => val1
[1] => val2
[2] => val3
[3] => val4
[4] => val5
[5] => val6
)
Thanks in advance!
I would say urlencode the double quotes when generating that url. Because link will result in the url you go to only being localhost/index.php?data=val1,val2,val3,
So like:
echo 'link';
Have you tried using explode? It'll separate a string into an array using whatever separator you specify.
Using your example,
$_GET['data'] = 'val1,val2,val3,"val4","val5",val6';
$testarr = explode(",", $_GET['data']);
var_dump($testarr);
Outputs:
array(6) {
[0]=>
string(4) "val1"
[1]=>
string(4) "val2"
[2]=>
string(4) "val3"
[3]=>
string(6) ""val4""
[4]=>
string(6) ""val5""
[5]=>
string(4) "val6"
}
Looking at your question again, it seems you might want to remove the " from $_GET['data'] entirely?. If so, do this:
$testarr = explode(",", str_replace('"','',$_GET['data']));
Hello i have this string which is an serial of code/references for things:
<?PHP
$list="1010<1>;1020<?>;3010<?>"; the list of items code
$id="5060<?>"; //will add this
$list_unique=explode(";", $list);
print_r($list_unique);
?>
Now the output is: Array ( [0] => 1010<1> [1] => 1020 [2] => 3010 )
Why? it forgots the part why? it should be
Array ( [0] => 1010<1> [1] => 1020<?> [2] => 3010<?> )
You're probably viewing the output as rendered HTML. View source and you'll see it isn't missing.
Alternatively, escape your output when you inspect it.
echo htmlspecialchars( print_r($list_unique, 1 ) );
Always var_dump($yourvar); instead of print_r($yourvar); if you are unsure of your results.
var_dump($list_unique); gave me this
array(3) {
[0]=>
string(7) "1010<1>"
[1]=>
string(7) "1020<?>"
[2]=>
string(7) "3010<?>"
}
I have the following code:
preg_match_all('/(.*) \((\d+)\) - ([\d\.\d]+)[,?]/U',
"E-Book What I Didn't Learn At School... (2) - 3525.01, FREE Intro DVD/Vid (1) - 0.15",
$match);
var_dump($string, $match);
and get the following ouput:
array(4) {
[0]=>
array(1) {
[0]=>
string(54) "E-Book What I Didn't Learn At School... (2) - 3525.01,"
}
[1]=>
array(1) {
[0]=>
string(39) "E-Book What I Didn't Learn At School..."
}
[2]=>
array(1) {
[0]=>
string(1) "2"
}
[3]=>
array(1) {
[0]=>
string(7) "3525.01"
}
}
which matches only one items... what i need is to get all items from such strings. when i've added "," sign to the end of the string - it worked fine. but that is non-sense in adding comma to each string. Any advice?
Try this regex:
(.*?)\s*\((\d+)\)\s*-\s*(\d+\.\d+)(?:,\s*)?
The major difference is that you had .* (greedy) which I replaced with .*? (un-greedy). Yours first "ate" the entire string (except line breaks) and then back tracked to match just one piece from your string.
Demo:
preg_match_all('/(.*?)\s*\((\d+)\)\s*-\s*(\d+\.\d+)(?:,\s*)?/',
"E-Book What I Didn't Learn At School... (2) - 3525.01, FREE Intro DVD/Vid (1) - 0.15",
$matches, PREG_SET_ORDER);
print_r($matches);
produces:
Array
(
[0] => Array
(
[0] => E-Book What I Didn't Learn At School... (2) - 3525.01,
[1] => E-Book What I Didn't Learn At School...
[2] => 2
[3] => 3525.01
)
[1] => Array
(
[0] => FREE Intro DVD/Vid (1) - 0.15
[1] => FREE Intro DVD/Vid
[2] => 1
[3] => 0.15
)
)