如何将字符串转换为数组字符

时间:2017-06-24 04:06:07

标签: php

第一行代码转换为第二行代码。我可以使用填充方法,但我希望从最短路径获得解决方案并快速转换。我将使用sql select部分中的代码字段

 $it = "(a(am,bam),b(dam,cam))";
 $to = "a.am, a.bam, b.dam, b.cam";

3 个答案:

答案 0 :(得分:5)

请尝试以下代码:

iParser

它解析括号的每个深度。只需将您的字符串传递给import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.View; import android.webkit.ValueCallback; import android.widget.FrameLayout; import android.widget.ProgressBar; import org.xwalk.core.XWalkPreferences; import org.xwalk.core.XWalkResourceClient; import org.xwalk.core.XWalkUIClient; import org.xwalk.core.XWalkView; import static com.github.crazyorr.embeddedcrosswalk.R.string.url; public class MainActivity extends AppCompatActivity { private XWalkView mXWalkView; private FrameLayout frameLayout; private SwipeRefreshLayout swipe; private ProgressBar progressBar; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frameLayout = (FrameLayout) findViewById(R.id.framelayout); progressBar = (ProgressBar) findViewById(R.id.ProgressBar); swipe= (SwipeRefreshLayout) findViewById(R.id.swipe); swipe.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue, R.color.purple); swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() {mXWalkView.reload(url);} }); loadweb(); } public void loadweb(){ mXWalkView = (XWalkView) findViewById(R.id.webview); mXWalkView.setResourceClient(new ResourceClient(mXWalkView)); mXWalkView.setUIClient(new XWalkUIClient(mXWalkView) { @Override public void onPageLoadStarted(XWalkView view, String url) { frameLayout.setVisibility(View.VISIBLE); progressBar.setVisibility(ProgressBar.VISIBLE); } @Override public void onPageLoadStopped(XWalkView view, String url, XWalkUIClient.LoadStatus status) { progressBar.setVisibility(view.GONE); swipe.setRefreshing(false); } }); XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true); mXWalkView.getSettings().setJavaScriptEnabled(true); mXWalkView.loadUrl("https://www.google.com.sa"); } class ResourceClient extends XWalkResourceClient { public ResourceClient(XWalkView xwalkView) { super(xwalkView); } public void onProgressChanged(XWalkView view, int progressInPercent){ frameLayout.setVisibility(View.VISIBLE); progressBar.setProgress(progressInPercent); if (progressInPercent == 100){ frameLayout.setVisibility(View.GONE); } super.onProgressChanged(view, progressInPercent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public void onBackPressed() { mXWalkView.evaluateJavascript("onBackPressed()", new ValueCallback<String>() { @Override public void onReceiveValue(final String value) { if (!Boolean.valueOf(value)) { MainActivity.super.onBackPressed(); } } }); } } 函数。

答案 1 :(得分:2)

试试这个:

<?php

function str2Arr( $s, $r, $str) {

  $str = trim( str_replace( $s, $r, $str ));
  return explode(" ", $str);
}

$it = "(a(am,bam),b(dam,cam))";
//$to = "a.am, a.bam, b.dam, b.cam";


$search = ['),', '(',  ')', ',', 'a a', 'b d', 'ba', 'c'];

$replace =[' ',  ' ',  ' ', ' ', 'a.a','b.d', 'a.ba', 'b.c'];

var_dump( implode(", ",( str2Arr( $search, $replace, $it) ) ) );

请参阅http://coussej.github.io/2016/01/14/Replacing-EAV-with-JSONB-in-PostgreSQL/

在不使用正则表达式的情况下,可以使用str_replace()来实现指定的转换,该str_replace()使用在主题字符串中找到的另一个字符数组替换字符数组。非字母字符每个都用空格替换,并且子串被替换,以便每个字符以&#34; a&#34;或&#34; b&#34;适当的后跟一个句点和子串的其余部分。

答案 2 :(得分:0)

现在杀了我。这是一个更好的方式,但我的眼睛正在流血。

如果我开始使用正则表达式,它可能会有更好的效果,但我的正则表达式技能已经生锈了。

我一直用锤子敲击你的数据,直到方形钉穿过圆孔。

<?php

 $it = "(a(am,bam),b(dam,cam))";
 $it = substr($it, 1, -1);
 //$to = "a.am, a.bam, b.dam, b.cam";

$final = [];

$test = explode("),", $it);
foreach($test as $section) {
    $letter = substr($section, 0, 1);
    $remainder = substr($section, 2);
    if(strpos($remainder, ")")) {
        $remainder = substr($remainder, 0, strpos($remainder, ")"));
    }
    echo $letter . " ".$remainder."\n";
    foreach(explode(",", $remainder) as $fragment) {
        array_push($final, $letter.".".$fragment);
    }
}

var_dump($final);

var_dump(implode($final, ", "));

<强>产量

a am,bam
b dam,cam
array(4) {
  [0]=>
  string(4) "a.am"
  [1]=>
  string(5) "a.bam"
  [2]=>
  string(5) "b.dam"
  [3]=>
  string(5) "b.cam"
}
string(22) "a.am, a.bam, b.dam, b.cam"