普通网友 2014-05-07 21:54
浏览 78
已采纳

PHP从数组内的字符串元素中删除括号

I am using in_array() function and trim() in order to detect strings in my array that include parenthesis (). Although that my current program is operating and meets my needs I would like to take it to the next level and make it more generic. Currently I am specifying the array position through "stack[2]".

$stack = array( "test-1" , "test-2" , "(test-3)" );

print_r($stack);

if ( in_array( "".$stack[2]."" , $stack , TRUE ) ) {
  $stack[2] = trim($stack[2], '()');
}

print_r($stack);

Before the trimming process:

Array
(
    [0] => test-1
    [1] => test-2
    [2] => (test-3)
)

After the trimming process:

Array
(
    [0] => test-1
    [1] => test-2
    [2] => test-3
)

I would like my program to search each value in the array, remove the () and save it on the same position. Sample of a test array is provided below:

$stack = array( "test-1" , "test-2" , "(test-3)" , "test-4" , "(test-5)" );

I am sure there is a way but I can not figure it how to do it.

Thank you for your time and effort.

  • 写回答

3条回答 默认 最新

  • dongmi3203 2014-05-07 22:12
    关注

    Use a for loop and sizeof array:

    $stack = array( "test-1" , "test-2" , "(test-3)" , "test-4" , "(test-5)" );
    
    print_r($stack);
    
    for ($i = 0; $i < sizeof($stack); $i++){
       $stack[$i] = trim($stack[$i], '()');
    }
    
    print_r($stack);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?