PHP 将包含括号的字符串拆分为 2 个变量

标签 php

我正在使用正则表达式 from虽然这只会提取括号内的文本,但我想完全删除它:

if( preg_match( '!\(([^\)]+)\)!', $text, $match ) )
    $text = $match[1];

例如我有:我的长文本字符串(带有另一个字符串)

如何获得:

$var1 = "my long text string";
$var2 = "with another string";

最佳答案

// This is all you need
<?php $data = explode('(' , rtrim($str, ')')); ?>

例子:

<?php
$str = 'your long text string (with another string)';
$data = explode('(' , rtrim($str, ')'));    

print_r($data);


// output 

// Array
// (
//     [0] => my long text string 
//     [1] => with another string
// )
// profit $$$$

?>

关于PHP 将包含括号的字符串拆分为 2 个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359273/

相关文章:

php - MySQL 通过匹配它们的 id 来组合两个表字段

php mysql单条记录展示

php - Facebook API : count attendees for event (limit problem)

javascript - Codeigniter 和 AJAX console.log(结果) 显示未定义

php - 通过 graph api 搜索 Facebook 用户视频?

php - ORM/Doctrine为ArrayCollections设置多个过滤器

php - MySQL搜索名字和姓氏

php - Jquery 时间戳和 php 时间戳之间的区别?

php - 在 Woocommerce 中结帐后更改订单总额

php - Sublimetext php-cs-fixer 将命名空间更改为小写