最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you split a string of pipe separated values and return an array? - Stack Overflow

programmeradmin0浏览0评论

I have an array like the following:

array(1) {
[0]=>
string(160)     "|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"
}

I'm trying to find a way to strip the pipes and turn them each into an array.

Here's the code that will output the results.

<?php 
if( in_array( 'gb',     get_field('rights_management_control_by_continent_europe') ) or 'gb' ==     get_field('rights_management_control_by_continent') ) {
?>   

STUFF HERE

<?php } ?>

And just out of curiosity, is this doable in JavaScript?

I have an array like the following:

array(1) {
[0]=>
string(160)     "|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"
}

I'm trying to find a way to strip the pipes and turn them each into an array.

Here's the code that will output the results.

<?php 
if( in_array( 'gb',     get_field('rights_management_control_by_continent_europe') ) or 'gb' ==     get_field('rights_management_control_by_continent') ) {
?>   

STUFF HERE

<?php } ?>

And just out of curiosity, is this doable in JavaScript?

Share Improve this question edited Mar 21, 2017 at 2:16 Emile Bergeron 17.4k5 gold badges85 silver badges132 bronze badges asked Mar 21, 2017 at 0:48 Billy Bragg in the UKBilly Bragg in the UK 1051 silver badge13 bronze badges 2
  • 1 Does <insert string here>.split("|"); work? – Feathercrown Commented Mar 21, 2017 at 0:50
  • 1 and in PHP you use explode – Thomas Commented Mar 21, 2017 at 0:58
Add a ment  | 

3 Answers 3

Reset to default 3

Use the PHP explode tag.

<?php
$arr = ["|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"];
$pieces = explode("|", $arr[0]);

Each item separated by the pipe symbol would be a new item in the away, with ad being [1] as you start with a pipe.

[ and ] can start and close an array

In Javascript you can split string to array by using

s = "a|b|c"
arr = s.split('|') 

//access your array
arr[0]
arr[1]
.....

So you have this array, I'll just put it in a variable $old_array:

$old_array = array(0=>"|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|");

To split the string on index 0 we use the explode function on the first element in the $old_array array:

$exploded_array = explode("|", $old_array[0]);

The variable $exploded_array will now hold an array with all the pairs of letters as separate elements:

["","ad","al","at","ax","ba","be",...]

In JavaScript it would look a little different but still similar:

var old_array = ["|ad|al|at|ax|ba|be|bg|by|ch|cz|de|dk|ee|es|eu|fi|fo|fr|gb|gg|gi|gr|hr|hu|ie|im|is|it|je|li|lt|lu|lv|mc|md|me|mk|mt|nl|no|pl|pt|ro|rs|ru|se|si|sj|sk|sm|tr|ua|va|"];

var split_array = old_array[0].split('|');

The split_array variable will contain all the pairs of letters as separate elements:

["","ad","al","at","ax","ba","be",...]
发布评论

评论列表(0)

  1. 暂无评论