将字符串转换为由方括号分隔的数组

时间:2018-11-28 06:58:06

标签: php arrays string

我有一个包含几个方括号的字符串。我需要将值作为数组用方括号分隔。 我的示例将使您理解:

$inputString = "['A'|'AA']['B'|'BB']['C'|'CC']";

我的目标是获得像这样的数组:

0=>'A'|'AA'
1=>'B'|'BB'
2=>'C'|'CC'

4 个答案:

答案 0 :(得分:2)

先删除后括号,然后使用public class RippleDrawable extends Drawable { private Paint mPaint; private Bitmap bitmap; private int rippleColor; private float mRipplePointX = 0; private float mRipplePointY = 0; private float mRippleRadius = 0; private int mAlpha = 200; private float mCenterPointX,mCenterPointY; private float mClickPointX; private float mClickPointY; //Maximum radius private float MaxRadius; //Starting radius private float startRadius; //End radius private float endRadius; //Record whether to raise your finger--->boolean private boolean mUpDone; //Record whether the animation is finished private boolean mEnterDone; /**Enter animation*/ //Enter the progress value of the animation private float mProgress; //Each incremental time private float mEnterIncrement = 16f/360; //Enter the animation add interpolator private DecelerateInterpolator mEnterInterpolator = new DecelerateInterpolator(2); private Runnable runnable = new Runnable() { @Override public void run() { mEnterDone = false; mCircleAlpha = 255; mProgress = mProgress + mEnterIncrement; if (mProgress > 1){ onEnterPrograss(1); enterDone(); return; } float interpolation = mEnterInterpolator.getInterpolation(mProgress); onEnterPrograss(interpolation); scheduleSelf(this, SystemClock.uptimeMillis() + 16); } }; /**How to enter the animation refresh * @parms realProgress */ public void onEnterPrograss(float realPrograss){ mRippleRadius = getCenter(startRadius,endRadius,realPrograss); mRipplePointX = getCenter(mClickPointX,mCenterPointX,realPrograss); mRipplePointY = getCenter(mClickPointY,mCenterPointY,realPrograss); mBgAlpha = (int) getCenter(0,182,realPrograss); invalidateSelf(); } private void enterDone() { mEnterDone = true; if(mUpDone) startExitRunnable(); } /**Exit animation*/ //Exit the progress value of the animation private float mExitProgress; //Each incremental time private float mExitIncrement = 16f/280; //Exit animation add interpolator private AccelerateInterpolator mExitInterpolator = new AccelerateInterpolator(2); private Runnable exitRunnable = new Runnable() { @Override public void run() { if (!mEnterDone){ return; } mExitProgress = mExitProgress + mExitIncrement; if (mExitProgress > 1){ onExitPrograss(1); exitDone(); return; } float interpolation = mExitInterpolator.getInterpolation(mExitProgress); onExitPrograss(interpolation); scheduleSelf(this, SystemClock.uptimeMillis() + 16); } }; /**Exit animation refresh method * @parms realProgress */ public void onExitPrograss(float realPrograss){ //Set background mBgAlpha = (int) getCenter(182,0,realPrograss); //Set the circular area mCircleAlpha = (int) getCenter(255,0,realPrograss); invalidateSelf(); } private void exitDone() { mEnterDone = false; } //Set the gradient effect including radius / bg color / center position, etc. public float getCenter(float start,float end,float prograss){ return start + (end - start)*prograss; } public RippleDrawable() { //Anti-aliased brush mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //Set anti-aliasing mPaint.setAntiAlias(true); //Set anti-shake mPaint.setDither(true); setRippleColor(0x60000000); } //private int mPaintAlpha = 255; //Background transparency private int mBgAlpha; //Transparency of the circular area private int mCircleAlpha; @Override public void draw(Canvas canvas) { //Get the transparency of user settings int preAlpha = mPaint.getAlpha(); //current background int bgAlpha = (int) (preAlpha * (mBgAlpha/255f)); //bg + prebg Operational background int maxCircleAlpha = getCircleAlpha(preAlpha,bgAlpha); int circleAlpha = (int) (maxCircleAlpha * (mCircleAlpha/255f)); mPaint.setAlpha(bgAlpha); canvas.drawColor(mPaint.getColor()); mPaint.setAlpha(circleAlpha); canvas.drawCircle(mRipplePointX,mRipplePointY,mRippleRadius,mPaint); //Set the initial transparency to ensure that the next entry into the operation will not go wrong mPaint.setAlpha(preAlpha); } public int getCircleAlpha(int preAlpha,int bgAlpha){ int dAlpha = preAlpha - bgAlpha; return (int) ((dAlpha*255f)/(255f - bgAlpha)); } public void onTouch(MotionEvent event){ switch (event.getActionMasked()){ case MotionEvent.ACTION_DOWN: //press mClickPointX = event.getX(); mClickPointY = event.getY(); onTouchDown(mClickPointX, mClickPointY); break; case MotionEvent.ACTION_MOVE: //move //onTouchMove(moveX,moveY); break; case MotionEvent.ACTION_UP: //up onTouchUp(); break; case MotionEvent.ACTION_CANCEL: //exit //onTouchCancel(); break; } } public void onTouchDown(float x,float y){ //Log.i("onTouchDown====",x + "" + y ); //unscheduleSelf(runnable); mUpDone = false; mRipplePointX = x; mRipplePointY = y; mRippleRadius = 0; startEnterRunnable(); } public void onTouchMove(float x,float y){ } public void onTouchUp(){ mUpDone = true; if (mEnterDone){ startExitRunnable(); } } public void onTouchCancel(){ mUpDone = true; if (mEnterDone){ startExitRunnable(); } } /** * Open to enter animation * */ public void startEnterRunnable(){ mProgress = 0; //mEnterDone = false; unscheduleSelf(exitRunnable); unscheduleSelf(runnable); scheduleSelf(runnable,SystemClock.uptimeMillis()); } /** * Open exit animation * */ public void startExitRunnable(){ mExitProgress = 0; unscheduleSelf(runnable); unscheduleSelf(exitRunnable); scheduleSelf(exitRunnable,SystemClock.uptimeMillis()); } public int changeColorAlpha(int color,int alpha){ //Set transparency int a = (color >> 24) & 0xFF; a = a * alpha/255; int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); int argb = Color.argb(a, red, green, blue); return argb; } //Take the center point of the drawn area @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mCenterPointX = bounds.centerX(); mCenterPointY = bounds.centerY(); MaxRadius = Math.max(mCenterPointX,mCenterPointY); startRadius = MaxRadius * 0.1f; endRadius = MaxRadius * 0.8f; } @Override public void setAlpha(int alpha) { mAlpha = alpha; onColorOrAlphaChange(); } @Override public int getAlpha() { return mAlpha; } @Override public void setColorFilter(ColorFilter colorFilter) { //Filter effect if (mPaint.getColorFilter() != colorFilter){ mPaint.setColorFilter(colorFilter); invalidateSelf(); } } @Override public int getOpacity() { //Return transparency if (mAlpha == 255){ return PixelFormat.OPAQUE; }else if (mAlpha == 0){ return PixelFormat.TRANSPARENT; }else{ return PixelFormat.TRANSLUCENT; } } public void setRippleColor(int rippleColor) { this.rippleColor = rippleColor; onColorOrAlphaChange(); } private void onColorOrAlphaChange() { //Set brush color mPaint.setColor(rippleColor); if (mAlpha != 255){ int pAlpha = mPaint.getAlpha(); int realAlpha = (int) (pAlpha * (mAlpha/255f)); //Set transparency mPaint.setAlpha(realAlpha); } invalidateSelf(); } } 函数。

explode

答案 1 :(得分:1)

尝试使用PHP爆炸功能。

$inputString = "['A'|'AA']['B'|'BB']['C'|'CC']";
$inputString=str_replace("["," ",$inputString);
print_r (explode("]",rtrim($inputString,"]")));

答案 2 :(得分:0)

尝试一下

$inputString = "['A'|'AA']['B'|'BB']['C'|'CC']";
$obj = str_replace("]","",str_replace("[","",explode("][", $inputString)));
print_r($obj);

答案 3 :(得分:0)

使用preg_match_all

$inputString = "['A'|'AA']['B'|'BB']['C'|'CC']";
preg_match_all('#\[(.*?)\]#', $inputString, $array);
print_r($array[1]);